Virtual destructors

Oct 18, 2009 at 1:53pm
Hi,

I understand that, when you delete a derived object, first the derived destructor, then the base destructor is called. But let's assume we have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Base 
{
public:
Base(int var1, int var2);
virtual ~Base();

void DoSomething() = 0;

private:
std::string string1;
std::string string2;
};

class Derived : public Base
{
public:
Derived(int var1, int var2);
void DoSomething();

private:
float float1;
};


So there is a virtual destructor in the base class, but none in the derived class. Why would you do this and what will happen now?

Thank you!
Last edited on Oct 18, 2009 at 1:57pm
Oct 18, 2009 at 2:10pm
This is indeed the way it should be - the destructor declared virtual in the base class.

It is to ensure when a Base is to be destroyed, it is properly done - the destructor of Derived is called first, then the dtor of Base.
Now, since you haven't provided a dtor for Derived, the default dtor is called.

If you mark a method as virtual in the bass class, it's not necessary that you also mark it as virtual in the derived class (but I think it's a good idea to do so, it makes the code easier to understand).
Oct 18, 2009 at 2:54pm
Hi,

Thank you for the answer. But what happens if I don't declare it virtual in the base class? Will it go wrong when destroying?
Oct 18, 2009 at 3:15pm
Yes, you can try making your destructors display something. You would see that the dtor of the derived class is not called if you don't say virtual in the base.

I don't have much idea what's happening behind =\ maybe the memory isn't handled properly...
Oct 18, 2009 at 3:33pm
Hi,

Thank you, I understand it.

But what about the constructors? Why don't you need to make these virtual?
Oct 18, 2009 at 3:38pm
Base constructors need to be called explicitly from derived classes constructors
Oct 18, 2009 at 5:02pm
Hey,

Can you explain that a bit; I don't really understand what you mean with that.

Thanks
Oct 18, 2009 at 5:16pm
Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class B
{
    protected:
        int i;

    public:
        B ( int i ) : i ( i ) {} // base constructor
};

class D : public B
{
     public:
        D ( int i ) : B ( i ) {} // derived constructor calling the base one

        D ( something ) // other derived constructor not calling the base constructor
        {
             //...
        }
};
Oct 18, 2009 at 5:51pm
Hi,

Yes, I understand that. But what if we have a situation like in my first example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Base 
{
public:
Base(int var1, int var2);
virtual ~Base();

void DoSomething() = 0;

private:
std::string string1;
std::string string2;
};

class Derived : public Base
{
public:
Derived(int var1, int var2);
void DoSomething();

private:
float float1;
};


The constructor of both the base and derived class is the same now - why would you do this and what happens now?

EDIT:
Nevermind, I understand it again. Thank you for the explaination.
Last edited on Oct 18, 2009 at 5:52pm
Oct 19, 2009 at 9:05am
If a base class's destructor is virtual, the derived class destructor is made virtual whether the virtual keyword is used or not.
Topic archived. No new replies allowed.