Lets say we have the following code
class Image
{
public:
Image(const std::string & path)
{
pBitmap_ = FreeImage_Load( imageFormat, pathname.c_str() );
}
~Image()
{
FreeImage_Unload(pBitmap_);
}
private:
FIBITMAP * pBitmap_;
};
How would Image(Image && rhs) be implemented?
after moving the dtor is still called on rhs, which will not give the intended effect?
I suppose something like
Image::Image( Image && rhs )
{
pBitmap_ = std::move(rhs.pBitmap_);
rhs.pBitmap_ = nullptr;
}
and then checking against null in the dtor should do the trick, but is there a better way?
The solution is to not do it yourself, but use the library facilities of the language:
Now you don’t need to write destructor, move constructor, or move assignment operator. Also your class automatically has its copy constructor and copy assignment operator deleted, so you don’t need to worry about those either, completing the Rule of Five.
An alternative is to specialize
default_delete, meaning that you don’t need to supply the deleter type tounique_ptr:This has global effects, so you should only do it if you’re confident that yours is the only code in the program that might perform this specialization.