C++ | Operator Overloading | Question 10
Which of the following operator functions cannot be global, i.e., must be a member function.
(A) new
(B) delete
(C) Converstion Operator
(D) All of the above
Answer: (C)
Explanation: new and delete can be global, see following example.
#include
#include
#include
using namespace std;
class Myclass {
int x;
public:
friend void* operator new(size_t size);
friend void operator delete(void*);
Myclass(int i) {
x = i;
cout << "Constructor called \n";
}
~Myclass() { cout << "Destructor called \n"; }
};
void* operator new(size_t size)
{
void *storage = malloc(size);
cout << "new called \n";
return storage;
}
void operator delete(void *p )
{
cout<<"delete called \n";
free(p);
}
int main()
{
Myclass *m = new Myclass(5);
delete m;
return 0;
}
Recommended Posts:
- C++ | Operator Overloading | Question 6
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 9
- C++ | Operator Overloading | Question 4
- C++ | Operator Overloading | Question 2
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 3
- C++ | Operator Overloading | Question 5
- Operator Overloading in C++
- Overloading New and Delete operator in c++
- Overloading Subscript or array index operator [] in C++
- C++ program to compare two Strings using Operator Overloading
- C++ Program to concatenate two strings using Operator Overloading
- Operator overloading in C++ to print contents of vector, map, pair, ..


