Virtual destructor when using boost

Everyone says that a destructor must be virtual when at least one of the class methods is virtual.
My questions are: is it right to say that the destructor must be virtual when using upstream?

class A {
public:

    ~A(){
        cout << "Destructor in A" << endl;
    }
};

class B: public A
{
public:

    ~B(){
        cout << "Destructor in B" << endl;
    }
};

int main()
{
    A* a = new B;
    cout << "Exiting main" << endl;
    delete a;
}

I do not have any virtual functions in this code, but if I do not make my base destructor virtual, it will not call the destructor B. And yes, I know that it makes no sense to use ucpasting if I do not have virtual functions.

Thank.

+3
source share
5 answers

the destructor must be virtual if at least one of the methods of the class is virtual

, - , , , , , , .

delete , undefined. , , .

Herb Sutter , (.. , ) public, virtual protected virtual. , ​​ , , , .

, , , , , , .

+6

, - . , .

+13

, , , , ( ) , ( UB).

+1

virtual, - ( ..) .

, ( ) , class virtual.

+1

:

shared_ptr<A> ptr = make_shared<B>();

B . @Neil Butterworth .

0
source

All Articles