이 자습서에서는 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