Originally Posted by
Clayg
object2+.(object4) and that adds the object 2 and 4 together. Is that right?
It depends on how you implement it. operator+ doesn't really has to add, it might also divide, print a message or do absolutely nothing.
Example of very simple class:
Code:
class C1
{
private:
int n;
public:
int operator+(const C1 &obj);
};
Now, let's define operator+ which actually adds member n of C1
Code:
int C1::operator+(const C1 &obj)
{
int addent1 = this->n;
int addent2 = obj.n;
int sum = addent1 + addent2;
cout << "Adding " << addent1 << " and " << addent2 << endl;
cout << "Result: " << sum << endl;
return sum;
}
And this one does totally something else
Code:
int C1::operator+(const C1 &obj)
{
this->n = -100000;
cout << "Ha ha ha, I've changed n to " << this->n << endl;
cout << "And result will always be 0 !" << endl;
return 0;
}