Re: Problem with virtual destructor
In article <gp294p$qcb$00$1@news.t-online.com>,
Rolf Magnus <ramagnus@t-online.de> wrote:
ZikO wrote:
BTW what is the purpose of having pure virtual destructor
class A {
virtual ~A() = 0;
};
and then definition of it outside the class?
A::~A() {}
Making the destructor pure virtual instead of just virtual has the same
effects as it has for normal member functions:
- you force derived classes to explicitly define a destructor
No. See below.
- your base class becomes abstract
True.
The derived class need not explicitly define a destructor.
If it doesn't, the compiler automatically creates a default
destructor for it.
You must however (as has been mentioned earlier in this thread)
provide a definition for the base class constructor, even if it's
declared pure virtual.
class Base {
public:
virtual ~Base() = 0;
};
Base::~Base() {} /* although pure virtual, definition must be provided */
class Derived : public Base {
/* no explicit destructor needed */
};
int main()
{
Derived x; /* instantiate a Derived object, no problem */
return 0;
}
If you omit the definition for ~Base, there will be a problem at
link time, no matter if you define ~Derived explicitly or not.