No that is not correct.
Binary operators take 2 parameters, one for each side of the operator. If you define your operator as part of a class, then the first parameters is automatically considered to be
this
, which means your function would only take one parameter.
An example of this:
1 2 3 4 5 6 7 8
|
class MyClass
{
MyClass operator + (const MyClass& rhs) const
{
// here, '*this' is the left hand side of the + operator
// and 'rhs' is the right hand side
}
};
|
If the operator is defined globally, the 'this' pointer does not exist. Therefore both left and right sides must be passed to the function:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class MyClass
{
// .. operator not in the class
};
//defined globally (outside the class)
MyClass operator + (const MyClass& lhs, const MyClass& rhs)
{
// here, there is no '*this' so instead...
// 'lhs' is the left side
// and 'rhs' is the right side
}
|
Note that in each case, the object being assigned to the sum is not represented. That is...
c = a + b;
.... only a and b are represented. c is not part of the + operator at all.
Also note that I don't return a reference in my examples, I return a copy (value). This is because you do not want to modify 'this'.. but rather you want to create an entirely new object and return that.
Finally... this is nonsense:
v3.operator+(v1,v2);
The proper way to do it would be like this:
v1 would be either 'this' or 'lhs' depending on whether the operator is global or not. And v2 would be 'rhs'. The new object that is generated by the + operator is returned, then gets assigned to v3 by the assignment operator.