Operator overloading should follow the following syntax:
It sounds to me like you are wanting to modify the instance itself inside the + operator overload. I would strongly advice against that. It would mean that this addition:Code:MyClass operator+(const SomeOtherClass &value) { MyClass result; // calculate new values for result based on the addition of this instance and 'value' return result; }
Would actually alter the "myStudent" instance. An addition between two operands should not modify the operands, that is, they should stay the same after the addition has taken place.Code:Student myStudent; myStudent.feespaid = 0; myStudent + 5.0;
So, if anything, what you really want to overload is the += operator:
Altough I feel that an addition between a Student and a float is abit illogical, atleast now it doesnt violate the "rules" of additionCode:Student& operator+=(const float value) { this->feespaid += value; return *this; }![]()




Reply With Quote