Open In App
Related Articles

C++ | Operator Overloading | Question 10

Improve Article
Improve
Save Article
Save
Like Article
Like

How can we restrict dynamic allocation of objects of a class using new?
(A) By overloading new operator
(B) By making an empty private new operator.
(C) By making an empty private new and new[] operators
(D) By overloading new operator and new[] operators


Answer: (C)

Explanation: If we declare new and [] new operators, then the objects cannot be created anywhere (within the class and outside the class)
See the following example. We can not allocate an object of type Test using new.

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class Test {
private:
    void* operator new(size_t size) {}
    void* operator new[](size_t size) {}
};

int main()
{
    Test *obj = new Test;
    Test *arr = new Test[10];
    return 0;
}


Quiz of this Question

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads