একটি ডেরাইভড ক্লাস অবজেক্টকে বেস ক্লাসের পয়েন্টার দিয়ে ডিলিট করার সময়, বেস ক্লাসে ভার্চুয়াল ডেস্ট্রাক্টর ডিফাইন করা উচিত।
#includeusing namespace std; class b { public: b() { cout << "Constructing base \n"; } virtual ~b() { cout << "Destructing base \n"; } }; class d : public b { public: d() { cout << "Constructing derived \n"; } ~d() { cout << "Destructing derived \n"; } }; int main(void) { d *derived = new d(); b *bptr = derived; delete bptr; return 0; }
Constructing base Constructing derived Destructing derived Destructing base