You are not logged in.
Pages:: 1
#1 2016-01-19 05:27:58
How can we restrict dynamic allocation of objects?
Programming Languages Quizzes Operator Overloading
Question:
How can we restrict dynamic allocation of objects of a class using new?
Option A):
By overloading new operator
Option B):
By overloading new operator and new[] operators
Option C):
By making an empty private new and new[] operators
Option D):
By making an empty private new operator.
Correct Answer is Option C):
By making an empty private new and new[] operators
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;
}
Online Web Tutorials And Interview Questions With Answers Forum:
https://globalguideline.com/forum/
Offline
2016-01-19 05:27:58
- Advertisement
- Ads By Google
Re: How can we restrict dynamic allocation of objects?
\n