Question
Option 2 : Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं
Detailed Solution
सही उत्तर Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं है।
Question:
In C++, what happens when a derived-class object is deleted through a base-class pointer and the base class destructor is NOT virtual? / C++ में जब बेस-क्लास पॉइंटर से डेरिव्ड-क्लास ऑब्जेक्ट delete किया जाए और बेस क्लास का destructor virtual न हो, तो क्या होता है?
Correct Answer: Only the base-class destructor runs; derived-class resources leak / केवल बेस-क्लास destructor चलता है; डेरिव्ड-क्लास के संसाधन लीक होते हैं
Without a virtual destructor, deleting through a base pointer invokes only the base destructor (static binding) — the derived destructor never runs, so resources owned by the derived part leak; the behaviour is actually undefined. The trap is assuming polymorphism applies to destruction automatically. / virtual destructor के बिना बेस पॉइंटर से delete करने पर केवल बेस destructor चलता है (static binding) — डेरिव्ड destructor कभी नहीं चलता, अतः उसके संसाधन लीक होते हैं; व्यवहार वस्तुतः undefined है। जाल यह मानना है कि destruction में polymorphism स्वतः लागू होता है।