When should i make the destrucor virtual.Is it when I have virtual functions in my class.
But if I dont make the destructor virtual it still works!
When should i make the destrucor virtual.Is it when I have virtual functions in my class.
But if I dont make the destructor virtual it still works!
Your destructor should be virtual if the class will be derived from. Here's an example where the destructor of a base class is not declared virtual:
Code:#include <iostream> class Base { public: ~Base() { std::cout<<"Base::~Base\n"; } private: }; class Derived: public Base { public: ~Derived() { std::cout<<"Derived::~Derived\n"; } }; int main() { { Derived d1; Base* d2 = new Derived; delete d2; //only base destructor called //this is not the behavior we want } //derived and base destructor called for d1 }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
When your child class derives from a base class and you are using pointers to allocate your classes
What doesn't get called here? That is no good now we have a memory leak.Code:#include <iostream> class CBaseClass { public: CBaseClass() : mVal(10){} ~CBaseClass() { std::cout<<"Base Destructor"; std::cin.get(); }; protected: int mVal; }; class CChildClass : public CBaseClass { public: CChildClass() { mPtr = new int; } ~CChildClass() { std::cout<<"Child Destructor"; delete mPtr; std::cin.get(); } private: int *mPtr; }; int main() { CBaseClass *childClass = new CChildClass(); delete childClass; }
Now the proper way:
What happens here. Looks like everything got clean up and was called.Code:#include <iostream> class CBaseClass { public: CBaseClass() : mVal(10){} virtual ~CBaseClass() { std::cout<<"Base Destructor"; std::cin.get(); }; protected: int mVal; }; class CChildClass : public CBaseClass { public: CChildClass() { mPtr = new int; } virtual ~CChildClass() { std::cout<<"Child Destructor"; delete mPtr; std::cin.get(); } private: int *mPtr; }; int main() { CBaseClass *childClass = new CChildClass(); delete childClass; }
Woop?
>> Your destructor should be virtual if the class will be derived from.
Publically derived from and used polymorphically. There are some cases where classes can be publically derived from but are not intended to be used polymorphically, so the virtual destructor is not necessary.
Also, in JaWiB's example when you delete d2 (or in prog-bman's first example), the result is undefined behavior. It might actually work. It might also crash. If it does work on your machine this time, you cannot rely on that, and should therefore always make the destructor virtual if the class is intended to be a base class that is used polymorphically.
Probably worth noting that none of the standard library classes have virtual destructors (to the best of my knowledge) so be careful if you decide to subclass them publicly.
Doesn't matter so much for private and protected inheritance.
Pardon me for interjecting on this point, but I think this isn't accurate if you mean that virtual destructors could be avoided if inheritance is private or protected.Doesn't matter so much for private and protected inheritance.
I think the importance is the same - could you explain?
Generally privately inherited classes are not used polymorphically, which is why it is not necessary to make the destructor virtual in those cases.