0

Destructor c++

The question from battles: https://code.sololearn.com/c1HuuzEtqQO7/?ref=app #include <iostream> using namespace std; class A { public: ~A(){ cout << "A" << endl; } }; class B:public A { ~B(){ cout << "b" << endl; } }; int main() { A* p = new B; delete p; return 0; } I have made object type B in heap. Why print just "A" on delete?

2nd Nov 2018, 5:05 AM
Александр Лебедев
Александр Лебедев - avatar
1 Odpowiedź
+ 1
It is because the destructor is not virtual. The pointer p points to a type A, so A is what gets "destructed".
2nd Nov 2018, 6:14 AM
Leif
Leif - avatar