Operator Overloading Quiz

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

Solution :

C) is correct.

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;
}

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--

--

Upskilling students for tech placements!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Leetcode] Partition Equal Subset Sum

Strap me to a dolly and call me Director: Dolly usage in Unity.

Container with Maximum Water

Ubuntu Desktop has failed me for the last time

Writing a HTTP Load Balancer in Python using TDD: Configuration

Revisiting Content Server browser performance

3-simple steps for database designing

Why to Learn Java in 2020?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Placewit

Placewit

Upskilling students for tech placements!

More from Medium

Google / Amazon / Meta Interview Question — LeetCode 1110

[Leetcode] Intersection of Two Arrays II

Leetcode — Distinct Subsequences II

836. Rectangle Overlap