Calling an object's constructor?
Hello!
I have a class mp_image, which looks something like this:
Code:
class mp_image {
int w, h;
byte* pixels; //byte is a typedef of uint8_t;
public:
mp_image()
{
w = h = 0;
pixels = NULL;
}
mp_image(int width, int height)
{
w = width;
h = height;
pixels = new byte[3*w*h];
}
~mp_image()
{
if (pixels) delete[] pixels;
}
mp_image& DownsampleNTimes(int n, mp_image& dest_image)
{
/* Initialize dest_image here with width w/n and height h/n */
/* more code... */
return;
}
}
It's a simplification, but anyway, I would like the member function DownsampleNTimes to take an object dest_image as an argument (by reference) to store the downsampled image in. dest_image must be an empty image (no need for calling it's destructor), and the function shall start by initializing the image so that the right amount of memory gets allocated for holding the pixels of the downsampled image. Is it possible to call the constructor (and even the destructor) of an object, or should objects always be refered to by pointers so that the constructors/destructor are called using the new/delete operators?