यह प्रश्न बहुत मूर्खतापूर्ण हो सकता है, हालांकि, मुझे कहीं और ठोस जवाब नहीं मिला है।ऑपरेटर हटाते समय कक्षा के विनाशक को एकाधिक विरासत में उपयोग किया जाता है
विरासत में उपयोग किए जाने वाले देर से बाध्यकारी कार्यों और वर्चुअल कीवर्ड के बारे में कम जानकारी के साथ।
कोड नमूना के रूप में, जब विरासत के मामले में जहां बेस क्लास पॉइंटर ढेर पर बनाए गए व्युत्पन्न क्लास ऑब्जेक्ट को इंगित करता है और ऑपरेटर को हटाता है, तो स्मृति को डिलीकेट करने के लिए उपयोग किया जाता है, व्युत्पन्न और आधार के विनाशक होंगे केवल तभी आदेश दिया जाता है जब बेस विनाशक वर्चुअल फ़ंक्शन घोषित किया जाता है।
अब मेरी सवाल यह है:
1) जब आधार के नाशक, आभासी नहीं है क्यों बुला ली गई dtor नहीं की समस्या हो ही जब का उपयोग कर के मामले में ऑपरेटर "हटाएँ", क्यों मामले में नहीं दी नीचे:
derived drvd;
base *bPtr;
bPtr = &drvd; //DTOR called in proper order when goes out of scope.
2) When "delete" operator is used, who is reponsible to call the destructor of the class? The operator delete will have an implementation to call the DTOR ? or complier writes some extra stuff ? If the operator has the implementation then how does it looks like , [I need sample code how this would have been implemented].
3) If virtual keyword is used in this example, how does operator delete now know which DTOR to call?
Fundamentaly i want to know who calls the dtor of the class when delete is used.
<h1> Sample Code </h1>
class base
{
public:
base(){
cout<<"Base CTOR called"<<endl;
}
virtual ~base(){
cout<<"Base DTOR called"<<endl;
}
};
class derived:public base
{
public:
derived(){
cout<<"Derived CTOR called"<<endl;
}
~derived(){
cout<<"Derived DTOR called"<<endl;
}
};
I'm not sure if this is a duplicate, I couldn't find in search.
int main() { base *bPtr = new derived();
delete bPtr;// only when you explicitly try to delete an object return 0;
}
छोटे बिंदु, लेकिन आपका प्रश्न एकाधिक विरासत कहता है। यह एकाधिक विरासत का एक उदाहरण नहीं है, यह सिर्फ सादा पुरानी विरासत है। – Rich