Base class destructor definition not required?

In all the examples I saw about polymorphism, the base class destructor virtualis defined with an empty body.

I am trying to understand this: why should it be an empty body? Why doesn’t it work if the method is declared as virtual, but not defined using the emtpy body? Wouldn't that be implemented by default destructor then? Or the fact that it is declared as virtualsuppresses even the default definition and forces the body to be explicitly defined?

Here is what I mean:

class A {
public:
    virtual void f();
    virtual ~A() {}
}

class B : public A {
public:
    ~B() {
        // destroy whatever
    }
}

Why can't it be declared ~A()just like this virtual ~A();without definition?

, ( ) ? destuctor virtual ~A() = 0, .

+4
3

~A() , virtual ~A(); ?

, , . . , . , . ++ 11.

virtual ~A() = default;

, ( ) ? destuctor , virtual ~A() = 0, .

. , , . , . .

class A {
// ...
    virtual ~A() = 0; // declare destructor as pure virtual
};
// ...
A::~A() {} // define destructor
+7

, , . , , . , , , .

( ), :

class Foo {
public:
    virtual ~Foo() = 0;
};

Foo::~Foo() {} //pre-C++11
Foo::~Foo() = default; //for an empty body with some benefits; C++11 and on
+1

, , , , .

++ 11 , - :

virtual ~Foo() = default;

- ( ), . ; , .

0

Source: https://habr.com/ru/post/1544650/


All Articles