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?
1 Resposta
+ 1
It is because the destructor is not virtual. The pointer p points to a type A, so A is what gets "destructed".