I have a class which allocates memory on the heap and then the destructor frees it. My copy constructor is never being called for some reason and I do not understand why. Here is my implementation:
AguiBitmap::AguiBitmap( const AguiBitmap &bmp )
{
this->nativeBitmapPtr = al_clone_bitmap(bmp.nativeBitmapPtr);
}
AguiBitmap::AguiBitmap( char *filename )
{
if(!filename)
{
nativeBitmapPtr = 0;
return;
}
nativeBitmapPtr = al_load_bitmap(filename);
if(nativeBitmapPtr)
{
width = al_get_bitmap_width(nativeBitmapPtr);
height = al_get_bitmap_height(nativeBitmapPtr);
}
else
{
width = 0;
height = 0;
}
}
ALLEGRO_BITMAP* AguiBitmap::getBitmap() const
{
return nativeBitmapPtr;
}
However, When I do something like:
AguiBitmap bitmap;
bitmap = AguiBitmap("somepath");
The copy constructor code is never called (set a breakpoint). And therefore, my issue of having an invalid pointer in the reconstructed object from the temporary object becomes invalid when the temporary one is destroyed.
What do I do to get my copy constructor to be called?
Thanks
That bit of code wont invoke the copy constructor - it invokes the assignment operator (or the copy-assignment operator):
// a helper `swap` function
void AguiBitmap::swap(AguiBitmap& a, AguiBitmap& b)
{
using std::swap; // enable the following calls to come from `std::swap`
// if there's no better match
swap(a.nativeBitmapPtr, b.nativeBitmapPtr);
swap(a.width, b.width);
swap(a.height,b.height);
}
AguiBitmap::AguiBitmap& operator=( const AguiBitmap &rhs )
{
// use copy-swap idiom to perform assignment
AguiBitmap tmp(rhs);
swap( *this, tmp);
return *this;
}
Also note that your copy constructor is incomplete, since the height and width members aren't being copied:
width = bmp.width;
height = bmp.height;
AguiBitmap("somepath");
will invoke:
AguiBitmap::AguiBitmap( char *filename )
and the assignment will invoke the assignment operator
to invoke your copy constructor, do this:
AguiBitmap bitmap;
AguiBitmap anotherBitmap(bitmap)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With