Question

I am working through some university tutorials and I have to delete this array I have made with pointers and I should see the deconstructors for each object in the array called as I delete it. Here is the code. (Csprite is the base class, CpowerPill, Cghost and Cpacman are derived classes)

    Csprite * psprites[3]; 

psprites[0]=new CpowerPill;
psprites[1]=new Cghost;
psprites[2]=new Cpacman;

delete psprites[0,1,2];


    cin >> endPro;

All I get when I run this a single deconstructor call from the base class when I should get 3 different deconstructors all from the respective derived classes. Any help would be really appreciated.

Was it helpful?

Solution

[I'm assuming that your code compiles; i.e. Csprite is the base class]

Since you've used new separately on each element, you need to use delete separately on each element:

delete psprites[0];
delete psprites[1];
delete psprites[2];

The comma notation that you've used in delete psprites[0, 1, 2]; doesn't do what you think it does. 0, 1, 2 is just an expression with value 2, so all you're doing is deleting the final element.

As the array psprites is stack-allocated, you don't need to delete that. In fact, to do so would be undefined behaviour.

One more thing, you need to ensure that Csprite has a virtual destructor, or you'll leak memory. You can do that by writing virtual ~Csprite(){} in its class declaration.

OTHER TIPS

The syntax [0,1,2] doesn't do what you want, because the comma-operator simply returns it's last argument. You have to call delete on each element:

delete psprites[0]
delete psprites[1]
delete psprites[2]

This is typically done in a for loop.

You need to delete each element. You could do that manually:

delete psprites[0];
delete psprites[1];
delete psprites[2];

Or you could iterate through them with a loop.

The reason your current code doesn't work is because there's no such syntax as [0,1,2] for accessing multiple elements in a loop. The use of , is the comma operator, which has the value of its right operand. So what you've done is equivalent to delete psprites[2];. This is why you only get one destructor call.

Use following:

 for (int index = 0; index < 3; index++)
          delete psprites[index];

One of possible solutions can look the following way

#include <iostream>
#include <memory>
#include <algorithm>
#include <iterator>

int main()
{
    struct A
    {
        virtual ~A() { std::cout << "A::~A()" << std::endl; }
    };

    struct B : A
    {
        ~B() { std::cout << "B::~B()" << std::endl; }
    };

    struct C : A
    {
        ~C() { std::cout << "C::~C()" << std::endl; }
    };

    struct D : A
    {
        ~D() { std::cout << "D::~D()" << std::endl; }
    };

    A * a[] = { new A, new B, new C, new D };

    std::for_each( std::begin( a ), std::end( a ), std::default_delete<A>() );
}

As for your code then this statement delete psprites[0,1,2];is invalid. Inside the square brackets there is an expression with the comma operator. So this statement is equivalent to

delete psprites[2];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top