इस ट्यूटोरियल में, हम यह समझने के लिए एक प्रोग्राम पर चर्चा करेंगे कि C++ में ऑब्जेक्ट्स के डायनेमिक आवंटन को कैसे प्रतिबंधित किया जाए।
इसके लिए हम नए ऑपरेटर के कार्य को निजी रखेंगे ताकि गतिशील रूप से इसका उपयोग करके वस्तुओं का निर्माण न किया जा सके।
उदाहरण
#include <iostream>
using namespace std;
class Test{
//making new operator private
void* operator new(size_t size);
int x;
public:
Test() { x = 9; cout << "Constructor is called\n"; }
void display() { cout << "x = " << x << "\n"; }
~Test() { cout << "Destructor is executed\n"; }
};
int main(){
Test t;
t.display();
return 0;
} आउटपुट
Constructor is called x = 9 Destructor is executed