/*Child is inherited from Parent*/
class Parent {
public:
Parent() //Constructor
{
cout << "\n Parent constructor called\n" << endl;
}
protected:
~Parent() //Dtor
{
cout << "\n Parent destructor called\n" << endl;
}
};
class Child : public Parent
{
public:
Child() //Ctor
{
cout << "\nChild constructor called\n" << endl;
}
~Child() //dtor
{
cout << "\nChild destructor called\n" << endl;
}
};
int main()
{
Parent * p2 = new Child;
delete p2;
return 0;
}
यदि मैं Parent
के विनाशक वर्चुअल बना देता हूं, तो मुझे एक त्रुटि प्राप्त होती है, तो संरक्षित विनाशक वर्चुअल बनाने का उद्देश्य क्या है?क्या संरक्षित विनाशक वर्चुअल बनाने के लिए कोई उपयोग है?
शायद हमें "डीटीआर संरक्षित क्यों कर देगा?" से शुरू करना चाहिए। –
आप कभी विनाशक वर्चुअल क्यों बनाना चाहते थे? आपको * उद्देश्य * नहीं पता होना चाहिए? एक संरक्षित विनाशक का अर्थ है कि वस्तुओं को बेस पॉइंटर्स के माध्यम से नष्ट नहीं किया जाना चाहिए, इसलिए 'मुख्य' में कोड सादा गलत है। – thiton
देखें http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – user998692