Sign in to follow this  

Couple of simple questions

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I just had a couple of simple questions that I feel quite stupid to be asking as I should really know the answers [looksaround] Question 1: In C++ if I implement Class1 and a derived class Class2, should the destructor of Class2 free any dynamically allocated member variables of class1? I was under the assumption that unless Class1 has a virtual destructor the calling the destructor for Class2 would in turn call the destructor for class1, is this correct? Question 2: Given
int num = 10;

While( num )
{
    num--;
}
Will this kind of while loop simply stay active until num = 0? So if num was 0 to start with the while will simply be skipped? Thanks for any help

Share this post


Link to post
Share on other sites
Question 1: NO, you allocated it on the heap so you need to get rid of it yourself.

Destructors destroy the class, just because the variable pointing the area of allocated memory doesn't exist any more doesn't mean something else could now be pointing to it and using it.

Question 2: Yes the loop will be skipped if the initial value is 0 and will stop on 0.


Hope this helps.

(edit: Sorry misread my answer was correct except for a misleading yes)

Share this post


Link to post
Share on other sites
Quote:
Original post by hoogie
Question 1:
In C++ if I implement Class1 and a derived class Class2, should the destructor of Class2 free any dynamically allocated member variables of class1?


No, not usually. For the most part, you should have each class clean up it's own data.

Quote:
I was under the assumption that unless Class1 has a virtual destructor the calling the destructor for Class2 would in turn call the destructor for class1, is this correct?


You should make Class1's destructor virtual. If all of your destructors in your class heirarchy are virtual, then deleting an instance of your object will call all of the correct destructors.


class A {~A(){printf("A deleted\n")};
class B: public A {~B(){printf("B deleted\n")};

class vA {virtual ~vA(){printf("vA deleted\n")};
class vB: public A {virtual ~vB(){printf("vB deleted\n")};

void func() {
A* a= new B();
vA* va = new vB();
delete a; // result: "A deleted\n" printed
delete va; // result: "vB deleted\nvA deleted\n" printed
}


Note that because I didn't make A's destructor virtual, detete a; did not call all of the destructors it should.

If your class is supposed to be derived from, you should make the destructor virtual.

Edit: fixed the details of the deletion prints

Quote:
Question 2:

Given

*** Source Snippet Removed ***

Will this kind of while loop simply stay active until num = 0? So if num was 0 to start with the while will simply be skipped?

Thanks for any help


Yes.

To make it very slightly more clear, you should possibly use:
while(0!=num)

instead. An old-C++ hand won't care which you use, but a newbie like you may be less confused.

(
I placed the constant on the left out of habit. It helps avoid things like:
while(num=0)
instead of
while(num==0)
. I'm paranoid. ~_~

)

Share this post


Link to post
Share on other sites
Quote:
Original post by hoogie
In C++ if I implement Class1 and a derived class Class2, should the destructor of Class2 free any dynamically allocated member variables of class1?


No, that responsibility belongs to Class1 - preferably Class2 doesn't even know they exist.

Quote:
I was under the assumption that unless Class1 has a virtual destructor the calling the destructor for Class2 would in turn call the destructor for class1, is this correct?


Class1 MUST have a virtual destructor. Only that way both destructors will get called in all cases, that is.

[Edited by - Sharlin on February 28, 2006 3:13:47 PM]

Share this post


Link to post
Share on other sites
With a virtual destructor:

Delete base via a base pointer: base destructor is called normally.
Delete derived via a derived pointer: base destructor is called, then derived destructor. The derived destructor therefore should not clean up any base members.
Delete derived via a base pointer: base destructor is called, then derived destructor. The derived destructor therefore should not clean up any base members.

With a non-virtual destructor:

Delete base via a base pointer: base destructor is called normally.
Delete derived via a derived pointer: base destructor is called, then derived destructor. The derived destructor therefore should not clean up any base members.
Delete derived via a base pointer: base destructor is called. Derived destructor IS NOT CALLED; bang you're dead (even if there are no extra members added by the derived object, I'm pretty sure this still falls in the realm of undefined behaviour, although perhaps one of the less-nasty-in-practice sorts of it).

As a rule, add a virtual destructor if you are adding any virtual member function. The presence of the latter indicates a desire to use polymorphism, which is therefore likely to require the former. It also won't generally cost you anything, because you already paid for the vtable.

More here

Share this post


Link to post
Share on other sites
All this virtual destructor stuff is kind of missing the main gist of the question (at least to my reading).

Regardless of vitual or not, if the destructor for class 2 gets run then the destructor for class 1 will also get run after class 2's code is done.

All the virtual does is ensure that class 2's version of the destructor will indeed be run regardless of how you are storing your reference to class 2.

Share this post


Link to post
Share on other sites

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this