• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

virtual destructors

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
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
  • 4
  • 4
  • 3
  • +1
1 Solution
 
jkrCommented:
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
 
DJ_AM_JuiceboxAuthor Commented:
ok so all I have to do is just:

class Base {
    virtual ~Base();
};

something like that?
0
 
AxterCommented:
>>"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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jkrCommented:
Yes, that's it. If the base class destructor is virtual, all derived destructors will be, too.
0
 
DJ_AM_JuiceboxAuthor Commented:
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
 
AxterCommented:
>>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
 
jkrCommented:
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
 
DJ_AM_JuiceboxAuthor Commented:
@ 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
 
DJ_AM_JuiceboxAuthor Commented:
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
 
jkrCommented:
>>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
 
AxterCommented:
>>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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now