C++ Interview: "Why make a destructor virtual?" (60s Answer)
Автор: CoddyBear
Загружено: 2025-11-05
Просмотров: 1215
Описание:
Base* p = new Derived(); delete p;
...So why is your C++ program still leaking memory? 🚨
💥 THE PROBLEM:
If the base destructor (~Base()) is NOT virtual, only ~Base() is called.
~Derived() is NEVER called, leaking all of the child's resources!
🚀 THE FIX:
Make the base class destructor virtual: virtual ~Base()
✅ Why?
A virtual destructor uses the vtable to ensure the 'correct' destructor (~Derived()) is called FIRST, followed by ~Base().
💡 RULE: In C++, if you use polymorphism, your base class destructor MUST be virtual.
Subscribe for more C++ tips!
#C++
#virtualdestructor
#MemoryLeak
#Programming
#Coding
#Developer
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: