What will print the following code?
#include <iostream>
using namespace std;

struct A {
    virtual ~A() = 0;
};
A::~A() { cout << "~A"; }

struct B : A {
    ~B() { cout << "~B"; }
};

int main() {
  A *a = new B;
  delete a;
}
Explanation
In C++ destructors are called recursively for each ancestor class from which this class extends, beginning from the destructor of this class. Since destructor of class A - virtual, then during a deletion, it will call a destructor B, so that C ++ will call a destructor A. Although destructor ~A() is declared as pure virtual, it must be defined.
See the details on http://stackoverflow.com/questions/999340/question-about-pure-virtual-destructor.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты