C ++ - why should we define a pure virtual destructor outside the class definition?

class Object { public: ... virtual ~Object() = 0; ... }; Object::~Object() {} // Should we always define the pure virtual destructor outside? 

Question: Should we always define a pure virtual destructor outside the class definition?

In other words, is this the reason why we should not define a virtual inline function?

thanks

+6
c ++
source share
2 answers

You can define virtual functions inline. You cannot define pure virtual inline functions.

The following syntax options are simply not allowed:

 virtual ~Foo() = 0 { } virtual ~Foo() { } = 0; 

But this is quite true:

 virtual ~Foo() { } 

You must define a pure virtual destructor if you intend to create one or subclasses, ref 12.4 / 7:

The destructor can be declared virtual (10.3) or pure virtual (10.4); if there are objects of this class, or any derived class is created in the program, the destructor must be defined. If a class has a base class with a virtual destructor, its destructor (independently declared or implicitly declared) is virtual.

+7
source share

A pure virtual function has no implementation by definition. It must be implemented by subclasses.

If you want to bind a destructor, just keep it virtual (not purely virtual).

0
source share

All Articles