C++ के Destructors
In this page:
class ClassName {
public:
~ClassName() {
// cleanup code, runs when the object is destroyed
}
};
Destructor क्या है?
एक destructor एक special member function है जो एक object scope से बाहर जाने या explicitly delete होने पर automatically चलता है।
यह class का नाम एक tilde (~) prefixed के साथ share करता है, कोई arguments नहीं लेता, और कोई return type नहीं है, और इसका काम यह clean करना है कि object क्या hold कर रहा था।
उदाहरण: What is a Destructor?
#include <iostream>
class Box {
public:
~Box() { std::cout << "Box destroyed" << std::endl; }
};
int main() {
Box b;
return 0;
} // destructor runs here
Login to try C/C++/Java/PHP code in the editor
Destructors और Resource Cleanup
Destructors एक object के own किए resources release करने के लिए essential हैं — सबसे common new से allocated memory।
अगर आप destructor के अंदर वह memory free नहीं करते, यह उस class के हर object destroy होने पर leak करती है, क्योंकि कुछ भी इसे कभी reclaim नहीं करेगा।
उदाहरण: Destructors and Resource Cleanup
// Include std::cout and std::cin
#include <iostream>
// Define the type Buffer
class Buffer {
int *data;
// Members below can be accessed from outside the class
public:
Buffer() { data = new int[10]; }
~Buffer() { delete[] data; std::cout << "Memory freed" << std::endl; }
};
// Program execution starts in main()
int main() {
Buffer b;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Implicit बनाम Explicit Destructors
अगर आप खुद एक destructor नहीं लिखते, compiler automatically एक implicit एक देता है।
वह default simple classes के लिए ठीक है, लेकिन जिस moment आपकी class heap memory या एक file handle जैसे एक raw resource manage करे, आपको इसे सही तरीके से release करने के लिए एक explicit destructor लिखना ज़रूरी है।
उदाहरण: Implicit vs Explicit Destructors
#include <iostream>
class Simple {
int value = 5; // no destructor written: compiler generates an implicit one
};
int main() {
Simple s;
std::cout << "No custom cleanup needed" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Destructor Order of Execution
जब कई objects साथ destroy होते हैं, जैसे local variables एक function के अंत में scope से बाहर जाना, वे उनकी creation के exact reverse order में destroy होते हैं — बनाया गया last object हमेशा destroy होने वाला पहला है, यह mirror करते हुए कि एक stack कैसे unwind होता है।
उदाहरण: Destructor Order of Execution
#include <iostream>
class Item {
int id;
public:
Item(int i) : id(i) {}
~Item() { std::cout << "Destroying item " << id << std::endl; }
};
int main() {
Item a(1);
Item b(2);
return 0; // b destroyed first, then a (reverse of creation order)
}
Login to try C/C++/Java/PHP code in the editor
Pointers के साथ Destructors
new से heap पर बनाए objects scope से बाहर जाने पर automatically destroy नहीं होते, क्योंकि एक raw pointer scope से बाहर जाना सिर्फ pointer को destroy करता है, जिसकी ओर यह point करता है उसे नहीं।
डेस्ट्रक्टर trigger करने और वह memory free करने के लिए आपको explicitly delete call करना होगा।
उदाहरण: Destructors with Pointers
#include <iostream>
class Box {
public:
~Box() { std::cout << "Box destroyed" << std::endl; }
};
int main() {
Box *ptr = new Box();
delete ptr; // must delete manually -- a raw pointer going out of scope does NOT call the destructor
return 0;
}
Login to try C/C++/Java/PHP code in the editor
obj.~Class()से manually एक destructor call करना, जो object के scope से बाहर जाने पर इसे दो बार चलने की ओर ले जाता है।- constructor में
newसे allocated memory कोdeleteकरना भूल जाना, एक leak cause करते हुए। - base destructor
virtualन होने पर एक base pointer के जरिए एक derived object delete करना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: