
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Restrict Dynamic Allocation of Objects in C++
In this tutorial, we will be discussing a program to understand how to restrict dynamic allocation of objects in C++.
For this we will be keeping the new operator function private so that objects cannot be created using it dynamically.
Example
#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; }
Output
Constructor is called x = 9 Destructor is executed
Advertisements