?
Solved

virtual destructors

Posted on 2007-08-07
12
Medium Priority
?
342 Views
Last Modified: 2013-11-05
Hi,

Going off a previous question, I have a few classes that look like:

class Base { public : virtual void DoSomething() = 0; };
class Derived_A : public Base { public : virtual void DoSomething() { cout << "A"; } };
class Derived_B : public Base { public : virtual void DoSomething() { cout << "B"; } };

and it was mentioned that:

"you should also have a virtual destructor which will make clean destruction while calling the destructor."

can someone explain what that means?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 19646489
A virtual destructor guarantees tha tthe base class destructors will be called in th eproper order, i.e. the base classes are given a chance to clean up properly. Making the base class destructor virtual ensures this behaviour.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 19646526
ok so all I have to do is just:

class Base {
    virtual ~Base();
};

something like that?
0
 
LVL 30

Expert Comment

by:Axter
ID: 19646571
>>"you should also have a virtual destructor which will make clean destruction while calling the destructor."

To add to jkr's comment, this is only an issue when deleting a type using a base type pointer.

You should use a virtual destructor anytime you think it's possible that the derived type will get deleted using a base type pointer.

Base *base = new DerivedA();
//do something here

delete base; //Deleting derive type using base pointer.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:jkr
ID: 19646591
Yes, that's it. If the base class destructor is virtual, all derived destructors will be, too.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 19646636
ah ok I see. Because I'm paranoid, can someone just verify this is correct:

class Base {
    virtual ~Base();
};

class Derived_A : public Base {
    // Now if I am not adding any code to  Derived_A's destructor, I have nothing to do, it will already
    // inherit as virtual, right?

    // But if I want to extend it, I have to mark it again as virtual:
    virtual ~Derived_A()
     {
           ....
      }
};

Thanks
0
 
LVL 30

Expert Comment

by:Axter
ID: 19646657
>>virtual ~Derived_A()

Your derived class doesn't need to have a virtual destructor.
If the base class has a virtual destructor, then all derived classes will have virtual destructor.
0
 
LVL 86

Expert Comment

by:jkr
ID: 19646670
Yes, that's correct. Yet you should always provide a destructor (even if empty), that helps not to "forget" any cleanup (speaking of best practises ;o)
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 19646765
@ Axter,

but what if I need to do some stuff in Derived_A's destructor - if I just define it like:

class Derived_A : public Base {
    ~Derived_A()
      {
            ....
      }
};

is it somehow still 'virtual' even though it isn't marked as such?

Thanks
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 19646770
I mean, is the virtual keyword redundant then in this case? Does it change anything just to rewrite 'virtual' again just as a reminder?
0
 
LVL 86

Expert Comment

by:jkr
ID: 19646788
>>I mean, is the virtual keyword redundant then in this case?

Yes, but it does not do any harm. It is just like with virtual methods like

class A {

virtual int foo () = 0;
};

class B: public A{

virtual int foo ();
};

or

class B: public A{

int foo ();
};

Both are equal.
0
 
LVL 30

Expert Comment

by:Axter
ID: 19646811
>>I mean, is the virtual keyword redundant then in this case? Does it change anything just to rewrite 'virtual' again just as a reminder?

Yes, it is redundant, if the base destructor/function is virtual.

However, there are many developers who will still mark the derived class virtual, just as a reminder to anyone maintaining the code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question