Why do one need to overload operators like new and delete. And also curious to know, why do people overload ‘[]’ operator?
Please let me know..
Thanks
Printable View
Why do one need to overload operators like new and delete. And also curious to know, why do people overload ‘[]’ operator?
Please let me know..
Thanks
new and delete are overloaded in case someone wanted to redefine the way memory was allocated. operator[] is overloaded for bounded arrays like a std::vector:
std::vector<int> a;
a.push_back(3);
std::cout << a[0] << std::endl; //prints out 3
You should be able to overload as many operators as possible so you have the flexibility to define them the way you want them to operate on your class.