PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup (डिस्ट्रक्टर (Destructor): सिंटैक्स (~), मेमोरी सफाई और कॉल क्रम) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: A destructor (~ClassName) is a parameterless special C++ member function invoked automatically when an object's lifetime ends to release heap memory, file handles, and other acquired resources. Destructors cannot be overloaded, always execute in the reverse order of construction, and must be declared virtual in polymorphic base classes to prevent resource leaks.
अध्याय सार: डिस्ट्रक्टर (~ClassName) बिना पैरामीटर वाला एक विशेष C++ मेंबर फंक्शन है जो ऑब्जेक्ट का जीवनकाल समाप्त होने पर स्वतः चलकर हीप मेमोरी, फाइल हैंडल्स और अन्य संसाधन मुक्त करता है। डिस्ट्रक्टर को ओवरलोड नहीं किया जा सकता, यह हमेशा कंस्ट्रक्शन के विपरीत क्रम में चलता है और पॉलीमॉर्फिक बेस क्लासेस में लीक से बचने के लिए virtual होना अनिवार्य है।
Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup: Detailed Notes & Architecture (डिस्ट्रक्टर (Destructor): सिंटैक्स (~), मेमोरी सफाई और कॉल क्रम)
1. Destructor Syntax and Language Constraints (डिस्ट्रक्टर सिंटैक्स और भाषा के नियम)
[EN] A destructor is declared with a tilde (~) immediately preceding the exact class name, taking zero arguments and having no return type (not even void): ~ClassName() {}. Because a destructor accepts no parameters, a class can have ONLY ONE destructor and destructors can NEVER be overloaded. Furthermore, a destructor cannot be declared static or const, and you cannot take its address. If a programmer omits writing a destructor, the compiler automatically synthesizes a trivial public inline destructor that invokes destructors for any member subobjects and base classes.
[HI] डिस्ट्रक्टर को क्लास के ठीक नाम से पहले टिल्ड (~) चिन्ह लगाकर घोषित किया जाता है; इसमें न तो कोई पैरामीटर होता है और न ही कोई रिटर्न टाइप (void भी नहीं): ~ClassName() {}। चूंकि यह कोई पैरामीटर नहीं लेता, इसलिए एक क्लास में केवल एक ही डिस्ट्रक्टर हो सकता है और इसे ओवरलोड नहीं किया जा सकता। डिस्ट्रक्टर को static या const घोषित नहीं किया जा सकता तथा इसका एड्रेस भी नहीं लिया जा सकता। यदि प्रोग्रामर डिस्ट्रक्टर न लिखे, तो कंपाइलर स्वतः एक डिफ़ॉल्ट डिस्ट्रक्टर बना देता है जो मेंबर और बेस ऑब्जेक्ट्स की सफाई करता है।
2. When Destructors Run and RAII Resource Cleanup (डिस्ट्रक्टर कब चलता है और RAII संसाधन सफाई)
[EN] Destructors run deterministically the instant an object leaves scope: when a local stack object reaches its closing brace '}', when delete (or delete[]) is called on a heap object, when a temporary object finishes its expression, or at program exit for static/global objects. Note that calling delete p on a pointer to a heap object runs ~ClassName() BEFORE freeing the raw heap memory; forgetting delete leaks both memory and destructor cleanup! This deterministic guarantee underpins RAII (Resource Acquisition Is Initialization)—acquiring resources (memory, FILE*, mutex locks, sockets) in the constructor and releasing them in the destructor so exceptions or early returns never leak resources.
[HI] ऑब्जेक्ट का दायरा (स्कोप) समाप्त होते ही डिस्ट्रक्टर तुरंत और निश्चित रूप से चलता है: जैसे लोकल ऑब्जेक्ट के ब्लॉक '}' बंद होने पर, हीप ऑब्जेक्ट पर delete/delete[] कॉल करने पर, या प्रोग्राम अंत में ग्लोबल/स्टैटिक ऑब्जेक्ट्स के लिए। हीप पॉइंटर पर delete p चलाने पर पहले ~ClassName() चलता है और उसके बाद मेमोरी मुक्त होती है; delete भूलने पर डिस्ट्रक्टर भी नहीं चलता। इसी निश्चित व्यवहार पर RAII (Resource Acquisition Is Initialization) आधारित है—जिसमें कंस्ट्रक्टर संसाधन प्राप्त करता है और डिस्ट्रक्टर उन्हें सुरक्षित रूप से मुक्त करता है।
3. Order of Destruction (Stack LIFO and Inheritance) (डिस्ट्रक्टर चलने का क्रम (स्टैक LIFO और इनहेरिटेंस))
[EN] Destructors ALWAYS execute in the exact reverse order of construction (Last-In, First-Out). Inside a block where objects A, then B, then C are constructed sequentially, destruction occurs in the order ~C(), then ~B(), then ~A(). Similarly, for an array Obj arr[3];, arr[2] is destroyed first, followed by arr[1], then arr[0]. In an inheritance hierarchy, construction proceeds Base -> Derived, whereas destruction proceeds strictly Derived -> Base (~Derived() body runs first, then member destructors in reverse declaration order, and finally ~Base()).
[HI] डिस्ट्रक्टर्स हमेशा कंस्ट्रक्शन के ठीक विपरीत क्रम (LIFO: जो सबसे अंत में बना, वह सबसे पहले नष्ट) में चलते हैं। यदि एक ब्लॉक में क्रमशः ऑब्जेक्ट A, B और C बनाए गए हों, तो समाप्ति पर पहले ~C(), फिर ~B() और अंत में ~A() चलेगा। इसी तरह Obj arr[3]; में पहले arr[2], फिर arr[1] और अंत में arr[0] नष्ट होता है। इनहेरिटेंस में कंस्ट्रक्शन Base से Derived की ओर होता है, जबकि डिस्ट्रक्शन ठीक उल्टा Derived से Base की ओर (~Derived() पहले और ~Base() बाद में) होता है।
4. Virtual Destructors and Exception Safety Rules (वर्चुअल डिस्ट्रक्टर और एक्सेप्शन सुरक्षा नियम)
[EN] Unlike constructors, destructors CAN and often SHOULD be virtual in base classes designed for polymorphic inheritance. If you delete a Derived instance through a Base pointer (Base *p = new Derived(); delete p;) when ~Base() is NOT virtual, C++ triggers undefined behavior—typically calling only ~Base() and leaking Derived's memory/handles! Declaring virtual ~Base() ensures dynamic dispatch calls ~Derived() first, followed automatically by ~Base(). Additionally, destructors are implicitly noexcept since C++11: allowing an uncaught exception to escape a destructor during stack unwinding immediately calls std::terminate() and aborts the program.
[HI] कंस्ट्रक्टर के विपरीत, पॉलीमॉर्फिक इनहेरिटेंस वाली बेस क्लास में डिस्ट्रक्टर virtual हो सकता है और होना भी चाहिए। यदि ~Base() वर्चुअल न हो और आप Base *p = new Derived(); delete p; लिखें, तो केवल ~Base() चलता है (अपरिभाषित व्यवहार) और Derived के संसाधन लीक हो जाते हैं। virtual ~Base() लगाने से रनटाइम पर पहले ~Derived() और फिर स्वतः ~Base() कॉल होना सुनिश्चित होता है। साथ ही C++11 से डिस्ट्रक्टर डिफ़ॉल्ट रूप से noexcept होते हैं; यदि स्टैक अनवाइंडिंग के दौरान डिस्ट्रक्टर से कोई एक्सेप्शन बाहर निकले तो std::terminate() प्रोग्राम बंद कर देता है।
Syntax, Formula & Technical Example (Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup)
#include <iostream>
class Buffer {
int *data;
public:
Buffer(int n) : data(new int[n]) { std::cout << "Alloc\n"; }
virtual ~Buffer() { delete[] data; std::cout << "Freed\n"; }
};
int main() {
Buffer b(10); // ~Buffer() runs automatically at scope exit
return 0;
}
Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup Quick Reference & Comparison Table
| Rule / Scenario | Destructor Behavior | Exam Key Point |
|---|---|---|
| Syntax & Overloading | ~ClassName() with 0 args, no return type | Only 1 destructor per class; cannot be overloaded |
| Block Order (A then B) | Destroys B first, then A (reverse/LIFO) | Same LIFO reverse rule for arrays arr[n-1]..arr[0] |
| Inheritance Order | ~Derived() executes BEFORE ~Base() | Opposite of Base() -> Derived() constructor order |
| Polymorphic Base | Must declare virtual ~Base() | Prevents leak when deleting Derived via Base* |
Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup Top 20 Bilingual MCQs with Answers
Q1. Which symbol precedes the class name when declaring a destructor in C++?
(C++ में डिस्ट्रक्टर घोषित करते समय क्लास नाम से ठीक पहले कौन-सा प्रतीक चिन्ह लगाया जाता है?)
(A) # (Hash)
(B) ~ (Tilde)
(C) & (Ampersand)
(D) ! (Exclamation)
Correct Answer: (B)
[EN] A C++ destructor is named ~ClassName() using the tilde operator symbol.
[HI] C++ डिस्ट्रक्टर के नाम से पहले टिल्ड (~) चिन्ह लगाया जाता है, जैसे ~ClassName()।
Q2. How many destructors can a single C++ class have?
(एक अकेली C++ क्लास में अधिकतम कितने डिस्ट्रक्टर हो सकते हैं?)
(A) At most one (destructors cannot be overloaded)
(B) One per constructor in the class
(C) Unlimited overloaded destructors
(D) Two (default and parameterized)
Correct Answer: (A)
[EN] Because destructors accept zero parameters, their signature cannot differ and overloading is impossible.
[HI] डिस्ट्रक्टर में कोई पैरामीटर नहीं होता, इसलिए उन्हें ओवरलोड नहीं किया जा सकता और एक क्लास में केवल एक ही डिस्ट्रक्टर होता है।
Q3. If local objects A, B, and C are constructed in that order inside a function, in which order are their destructors executed when the function returns?
(यदि एक फंक्शन के भीतर ऑब्जेक्ट A, B और C इसी क्रम में बनते हैं, तो फंक्शन लौटते समय उनके डिस्ट्रक्टर किस क्रम में चलेंगे?)
(A) ~B(), then ~C(), then ~A()
(B) Only ~C() executes
(C) ~C(), then ~B(), then ~A()
(D) ~A(), then ~B(), then ~C()
Correct Answer: (C)
[EN] Stack objects are destroyed in strict reverse order of construction (LIFO).
[HI] स्टैक ऑब्जेक्ट्स हमेशा निर्माण के ठीक विपरीत क्रम (LIFO) में नष्ट होते हैं: पहले ~C(), फिर ~B(), फिर ~A()।
Q4. In an inheritance hierarchy where Derived inherits from Base, what is the destructor execution sequence when a Derived object goes out of scope?
(इनहेरिटेंस में जब Derived ऑब्जेक्ट का स्कोप समाप्त होता है, तो डिस्ट्रक्टर्स किस क्रम में चलते हैं?)
(A) Only ~Derived() runs automatically
(B) ~Base() runs first, then ~Derived()
(C) ~Derived() runs first, then ~Base()
(D) Neither runs unless called explicitly
Correct Answer: (C)
[EN] Destruction reverses construction order: Base() -> Derived() construction corresponds to ~Derived() -> ~Base() destruction.
[HI] डिस्ट्रक्टर हमेशा कंस्ट्रक्टर के विपरीत चलते हैं: इसलिए पहले ~Derived() और उसके बाद ~Base() चलता है।
Q5. Why should a polymorphic base class declare its destructor as virtual?
(पॉलीमॉर्फिक बेस क्लास में डिस्ट्रक्टर को virtual घोषित करना क्यों आवश्यक होता है?)
(A) So the destructor can accept integer parameters
(B) So constructors also become virtual automatically
(C) So the compiler allocates objects on the stack instead of heap
(D) So deleting a Derived object through a Base* pointer invokes ~Derived() before ~Base()
Correct Answer: (D)
[EN] Without a virtual destructor, delete basePtr; only triggers non-virtual ~Base(), causing undefined behavior and leaking Derived resources.
[HI] वर्चुअल डिस्ट्रक्टर के बिना Base* से Derived ऑब्जेक्ट को delete करने पर केवल ~Base() चलता है और Derived की मेमोरी लीक हो जाती है।
Q6. Which keyword must be used to destroy an array of objects allocated via Item *p = new Item[10]; so that all 10 destructors execute?
(Item *p = new Item[10]; द्वारा बनाए गए ऑब्जेक्ट्स के ऐरे के सभी 10 डिस्ट्रक्टर्स चलाने के लिए किसका उपयोग करना अनिवार्य है?)
(A) p->~Item();
(B) free(p);
(C) delete[] p;
(D) delete p;
Correct Answer: (C)
[EN] Allocating with new[] requires matching deallocation with delete[] so all 10 element destructors run; plain delete causes undefined behavior.
[HI] new[] से बने ऐरे के लिए delete[] p; का प्रयोग आवश्यक है ताकि सभी 10 तत्वों के डिस्ट्रक्टर चलें; केवल delete p लिखना गलत है।
Q7. What happens if you create a heap object using FileGuard *fg = new FileGuard(); and never call delete fg; before main() exits?
(यदि आप FileGuard *fg = new FileGuard(); से हीप पर ऑब्जेक्ट बनाएं और कभी delete fg; कॉल न करें, तो क्या होता है?)
(A) The destructor ~FileGuard() is never invoked, leaking the heap memory and any files/locks inside it
(B) The destructor automatically runs when fg pointer leaves main()
(C) C++ garbage collector calls ~FileGuard() within 5 seconds
(D) The compiler inserts delete fg; automatically
Correct Answer: (A)
[EN] Raw pointers leave scope without triggering destructors on the pointed heap object unless explicitly deleted or wrapped in a smart pointer (std::unique_ptr).
[HI] रॉ पॉइंटर का स्कोप खत्म होने पर हीप ऑब्जेक्ट का डिस्ट्रक्टर स्वतः नहीं चलता; बिना delete या स्मार्ट पॉइंटर के मेमोरी व रिसोर्स लीक होते हैं।
Q8. What is the return type of a destructor ~Sample()?
(डिस्ट्रक्टर ~Sample() का रिटर्न टाइप क्या होता है?)
(A) int
(B) bool
(C) void
(D) No return type (not even void)
Correct Answer: (D)
[EN] Like constructors, destructors have no return type specification whatsoever.
[HI] कंस्ट्रक्टर की तरह ही डिस्ट्रक्टर का भी कोई रिटर्न टाइप नहीं होता (void भी नहीं)।
Q9. When is the destructor of a global or static storage object executed?
(ग्लोबल या स्टैटिक स्टोरेज ऑब्जेक्ट का डिस्ट्रक्टर कब चलता है?)
(A) After main() finishes (during program termination)
(B) Immediately after the first function call
(C) Never; static objects lack destructors
(D) At the start of main()
Correct Answer: (A)
[EN] Global and static local objects live for the entire program duration and have their destructors invoked upon normal termination after main() returns.
[HI] ग्लोबल और स्टैटिक ऑब्जेक्ट्स का जीवनकाल पूरे प्रोग्राम तक होता है और उनके डिस्ट्रक्टर्स main() के बाद प्रोग्राम समाप्ति पर चलते हैं।
Q10. What does RAII stand for in C++ resource management?
(C++ रिसोर्स मैनेजमेंट में RAII का पूर्ण रूप क्या है?)
(A) Recursive Array Initialization Interface
(B) Random Access Internal Index
(C) Resource Acquisition Is Initialization
(D) Runtime Allocation In Inheritance
Correct Answer: (C)
[EN] RAII binds resource lifetime to object lifetime: resources acquired in constructors are deterministically released in destructors.
[HI] RAII का अर्थ Resource Acquisition Is Initialization है, जहाँ कंस्ट्रक्टर संसाधन लेता है और डिस्ट्रक्टर उसे निश्चित रूप से मुक्त करता है।
Q11. What happens since C++11 if a destructor throws an uncaught exception while another exception is unwinding the stack?
(C++11 के बाद से यदि स्टैक अनवाइंडिंग के समय किसी डिस्ट्रक्टर से बिना पकड़ा गया एक्सेप्शन बाहर निकले, तो क्या होता है?)
(A) std::terminate() is called immediately, aborting the program
(B) The compiler ignores the exception inside destructors
(C) The destructor restarts from the first line
(D) The second exception silently replaces the first
Correct Answer: (A)
[EN] Destructors are implicitly noexcept(true) since C++11, so escaping exceptions terminate the process.
[HI] C++11 से डिस्ट्रक्टर स्वतः noexcept होते हैं; उनसे एक्सेप्शन बाहर निकलने पर std::terminate() प्रोग्राम को तुरंत रोक देता है।
Q12. Inside class Demo { File *f; Buffer b; }; where constructor opens f then b, in what order are f and b destroyed?
(class Demo { File *f; Buffer b; }; में पहले f और फिर b घोषित हैं; ऑब्जेक्ट नष्ट होते समय ~Demo() के बाद सदस्य किस क्रम में नष्ट होते हैं?)
(A) b is destroyed before f (reverse member declaration order), after ~Demo() body executes
(B) Both are destroyed before ~Demo() body starts
(C) f is destroyed before b, before ~Demo() body executes
(D) Random order depending on memory size
Correct Answer: (A)
[EN] First the ~Demo() body runs, then non-static data members are destroyed in reverse order of their declaration in the class.
[HI] पहले ~Demo() की बॉडी चलती है, फिर नॉन-स्टैटिक मेंबर्स क्लास में अपनी घोषणा के विपरीत क्रम (पहले b, फिर f) में नष्ट होते हैं।
Q13. When is an explicit manual destructor call like ptr->~MyClass(); genuinely used in C++?
(C++ में ptr->~MyClass(); द्वारा डिस्ट्रक्टर को सीधे (मैन्युअल) कब कॉल किया जाता है?)
(A) Before every return statement in main()
(B) Inside every default constructor
(C) Whenever freeing a normal stack variable
(D) Only when destroying an object constructed with placement new (new (buf) MyClass())
Correct Answer: (D)
[EN] Objects constructed in pre-allocated raw buffers via placement new cannot use delete (which would free raw memory), so ~MyClass() is called explicitly.
[HI] जब किसी पहले से आवंटित बफर में placement new द्वारा ऑब्जेक्ट बनाया जाए, तब मेमोरी फ्री किए बिना ऑब्जेक्ट नष्ट करने के लिए मैन्युअल कॉल होता है।
Q14. Which specifier is allowed on a destructor declaration?
(डिस्ट्रक्टर घोषणा के साथ निम्नलिखित में से कौन-सा कीवर्ड अनुमत है?)
(A) volatile
(B) const
(C) static
(D) virtual
Correct Answer: (D)
[EN] A destructor can be virtual (and inline/override/final/noexcept), whereas static, const, and volatile qualifiers are forbidden.
[HI] डिस्ट्रक्टर virtual हो सकता है, जबकि static, const या volatile कीवर्ड इसके साथ नहीं लगाए जा सकते।
Q15. Why does a pure virtual destructor (virtual ~Shape() = 0;) still require a function body implementation in C++?
(प्योोर वर्चुअल डिस्ट्रक्टर (virtual ~Shape() = 0;) होने पर भी C++ में इसकी फंक्शन बॉडी परिभाषित करना अनिवार्य क्यों होता है?)
(A) Because pure virtual functions always require bodies
(B) To enable destructor overloading
(C) Because every derived class destructor automatically invokes its base class destructor at the end of destruction
(D) So Shape objects can be instantiated directly
Correct Answer: (C)
[EN] Even if Base is abstract, destroying a Derived object still calls ~Base() at link time, so omitting the body of ~Shape() causes a linker error.
[HI] एब्स्ट्रैक्ट क्लास होने पर भी डिराइव्ड ऑब्जेक्ट नष्ट होते समय अंत में ~Shape() को कॉल करता ही है, इसलिए लिंकर एरर से बचने के लिए उसकी बॉडी लिखनी आवश्यक है।
Q16. Which of the following statements about C++ destructors is NOT true?
(C++ डिस्ट्रक्टर्स के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)
(A) A destructor name starts with '~' followed by the class name
(B) A destructor can take default arguments to perform partial cleanup
(C) Stack objects invoke destructors automatically upon leaving scope
(D) Base class destructors execute after derived class destructors
Correct Answer: (B)
[EN] Destructors cannot take any arguments at all—neither required nor default arguments.
[HI] डिस्ट्रक्टर में किसी भी प्रकार के पैरामीटर (डिफ़ॉल्ट पैरामीटर भी) नहीं हो सकते।
Q17. Within Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup, which rule / scenario directly maps to: "~ClassName() with 0 args, no return type"?
(Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup में कौन सा Rule / Scenario सीधे "~ClassName() with 0 args, no return type" से संबंधित है?)
(A) Syntax & Overloading
(B) Polymorphic Base
(C) Inheritance Order
(D) Block Order (A then B)
Correct Answer: (A)
[EN] In Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup, 'Syntax & Overloading' handles '~ClassName() with 0 args, no return type' (Only 1 destructor per class; cannot be overloaded).
[HI] Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup में 'Syntax & Overloading' का कार्य/विवरण '~ClassName() with 0 args, no return type' (Only 1 destructor per class; cannot be overloaded) है।
Q18. Which option accurately describes 'Block Order (A then B)' in Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup?
(Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup के संदर्भ में 'Block Order (A then B)' की सही विशेषता कौन सी है?)
(A) Destroys B first, then A (reverse/LIFO)
(B) ~ClassName() with 0 args, no return type
(C) ~Derived() executes BEFORE ~Base()
(D) Must declare virtual ~Base()
Correct Answer: (A)
[EN] 'Block Order (A then B)' provides 'Destroys B first, then A (reverse/LIFO)' — key point: Same LIFO reverse rule for arrays arr[n-1]..arr[0].
[HI] 'Block Order (A then B)' का मुख्य विवरण 'Destroys B first, then A (reverse/LIFO)' है (Same LIFO reverse rule for arrays arr[n-1]..arr[0])।
Q19. In Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup, identify the correct parameter for the specification: "~Derived() executes BEFORE ~Base()".
(Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup में "~Derived() executes BEFORE ~Base()" विवरण किस विकल्प के लिए सही है?)
(A) Polymorphic Base
(B) Block Order (A then B)
(C) Syntax & Overloading
(D) Inheritance Order
Correct Answer: (D)
[EN] 'Inheritance Order' corresponds to '~Derived() executes BEFORE ~Base()' (Opposite of Base() -> Derived() constructor order).
[HI] 'Inheritance Order' का संबंध '~Derived() executes BEFORE ~Base()' (Opposite of Base() -> Derived() constructor order) से है।
Q20. Considering Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup, what key behavior defines 'Polymorphic Base'?
(Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup में 'Polymorphic Base' की मुख्य भूमिका या मान क्या है?)
(A) ~Derived() executes BEFORE ~Base()
(B) Must declare virtual ~Base()
(C) Destroys B first, then A (reverse/LIFO)
(D) ~ClassName() with 0 args, no return type
Correct Answer: (B)
[EN] 'Polymorphic Base' represents 'Must declare virtual ~Base()' (Prevents leak when deleting Derived via Base*).
[HI] 'Polymorphic Base' का अर्थ/मान 'Must declare virtual ~Base()' है (Prevents leak when deleting Derived via Base*)।
Destructors in C++: Syntax (~ClassName), Rules and Resource Cleanup — Exam FAQs
Q1: Can I make a destructor private?
क्या मैं किसी डिस्ट्रक्टर को private बना सकता हूँ?
[EN] Yes; a private destructor prevents stack allocation (since callers can't destroy it when leaving scope) and forces objects to be heap-allocated and destroyed only via a dedicated member/friend function.
[HI] हाँ; प्राइवेट डिस्ट्रक्टर स्टैक पर ऑब्जेक्ट बनाना रोक देता है और ऑब्जेक्ट को केवल क्लास के विशेष मेथड या फ्रेंड द्वारा नष्ट किए जाने के लिए सीमित करता है।
Q2: What happens if I call delete on a nullptr in C++?
यदि C++ में nullptr पर delete कॉल किया जाए तो क्या होता है?
[EN] Deleting a null pointer is guaranteed safe and performs no operation (no-op); the destructor is not called and no crash occurs.
[HI] nullptr पर delete करना C++ मानक द्वारा पूरी तरह सुरक्षित (no-op) है; इससे कोई डिस्ट्रक्टर नहीं चलता और न ही प्रोग्राम क्रैश होता है।
Q3: Do I still need to write manual destructors in modern C++?
क्या आधुनिक C++ में भी हर क्लास में मैन्युअल डिस्ट्रक्टर लिखना आवश्यक है?
[EN] Often no—if your class uses RAII members like std::vector, std::string, or std::unique_ptr (Rule of Zero), the compiler-generated destructor cleans up everything automatically.
[HI] अक्सर नहीं—यदि आपकी क्लास std::vector, std::string या std::unique_ptr जैसे RAII मेंबर्स उपयोग करती है (Rule of Zero), तो डिफ़ॉल्ट डिस्ट्रक्टर सब कुछ स्वतः साफ कर देता है।
Post a Comment
Post a Comment