class A {
....
};
class B: public A {
int x;
...
};
class C: public A{
A * ptr;
.....
};
class D : public A {
A* ptr1;
A* ptr2;
....
};
Note: I made all the constructors for B,C,D just didn't include them in there. So A (with no fields) is the super class and I have 3 subclasses (B,C and D) each with different fields.
A is an abstract class and its mostly chaining of Class (B,C,D)
So like I might have a situation like
B *x = new B {5};
B *x2 = new B {5}
D * y = new D{x,x2);
So when I do delete y;
I want to make it chain destruct the 2 pointers of its two fields which are (B objects). How would I make the destructor for class D then chain destruct?
Like the example I show is really simple but other examples have more and more layers. I want to make sure that everything is deleted so no memory leaks occur.
Should my dtor for Class D look like this ?
~D(){
delete ptr1;
delete ptr2;
}
and for the case of class C would I just do this?
~C(){
delete ptr;
}
Because I did this and it doesnt work I get memory leaks so whats wrong?