Destructor c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 Answer
+ 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