class destructor and new and delete

Hey peoples,
This should probably be posted in beginner forum but it looks pretty busy in there. I didn't see anything in the tutorials that really talks about what I want to do, but might have missed something. Anyway thanks in advance for any help or suggestions yall can give me.

Let's say I have an abstract base class "c_unit", and derived class "c_matl". I've got a vector of type <c_unit*> "v_units", and when the user decides to terminate the program, it calls a function to go through "v_units" and delete all "c_unit" derived objects before clearing the vector.

My question is this: If c_matl or another derived class were to allocate memory, like another vector of pointers, will the default destructor of that derived class automatically delete all the new memory allocated by that object, or do I now have to define a destructor? In other words, when I run that function to clear the vector "v_units" will the delete on each c_unit* also delete any memory allocated by the object pointed to? I hope that question makes sense.

I have not defined a destructor for my base class or for any derived classes.. everything runs fine I just want to make sure I am not allocating memory that doesn't get freed up. Any guidance and suggestions are appreciated.

No, the destructor will not automatically call delete. You will have to define the destructor to do that.

Also make sure to declare the destructor of c_unit as virtual otherwise the destructor of c_matl will not get called properly when deleting a c_matl object through a c_unit pointer.
closed account (zb0S216C)
In addition to what Peter87 said, if you have to overload the default destructor, you're better of overloading both the copy-assignment operator and the copy-constructor, too. This is known as the Rule of Three[1, Rule of Three] which is defined as [by Wikipedia]:

"The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three:

● destructor
● copy constructor
● copy assignment operator

[1, Rule of Three]

K thanks guys
was trying to avoid a lot of stuff I clearly should be doing
Topic archived. No new replies allowed.