चलो कक्षा Test
और एक वर्ग AnotherClass
है। दोनों QObject से निकला।अपने विनाशक को परिभाषित करना हालांकि कक्षा QObject से निकली है?
Test.h:
class Test : public QObject
{
Q_OBJECT
public:
Test(QObject* parent);
~Test();
private:
AnotherClass* other;
};
class AnotherClass : public QObject
{
Q_OBJECT
public:
AnotherClass(QObject* parent);
~AnotherClass();
};
Test.cpp: जब Test
-instance नष्ट हो जाता है क्योंकि Test
other
के माता पिता, सही है
Test::Test(QObject* parent) : QObject(parent)
{
other = new AnotherClass(this);
}
Test::~Test()
{
delete other;
}
other
स्वचालित रूप से नष्ट कर दिया जाना चाहिए?
- अब मैं
~Test()
मेंother
अपने आप से हटा देना चाहिए या यह एक अपरिभाषित चरण में कार्यक्रम छोड़ने क्योंकि यह दो बार एक ही वस्तु को नष्ट करने की कोशिश करता है? - यहां सही दृष्टिकोण क्या है?
आपके विनाशक आभासी हो। – KoVadim
@ कोवाडिम - क्योंकि क्यूओब्जेक्ट में आभासी विनाशक है, इसलिए उनके विनाशक पहले से ही आभासी हैं .. – Roddy