Explain how the operator overload function works? What new objects are being created at this moment? How does the this pointer work?
|
1 answer
An operator is exactly the same function as any other, only with a predetermined priority and the number of arguments. So that
class X { X& operator + (int y); essentially no different than
class X { X& add(int y); Just what you can write
x.add(5); Can i
x + 5; or
x.operator+(5); Objects are created exactly the ones you specify to create. And this , as always, makes sense only in a member operator and points to an object of the class for which the statement is called.
- o3 = o1 + o2; will this indicate o1? - Artyom Arshakyan
- 2If the + operator is a member of a class, then this entry will be interpreted as
o3=o1.operator+(o2)and, as you wrote,thiswill point too1. - Harry
|