Original Post
hi all, I have a question about operator overloading and the implications of using auxiliary operators over member functions. For the sake of simplicity and argument I have a class, say CPosition, with x, y and z doubles. to add two positions together I do: which enables me to call the very (what I think is) efficient: I admit my knowledge of this area is shaky, so please point out any stupid mistakes i have made, but my query is basically along the lines of: can I make an operator that works just as fast as the member function? I know it is slower because it returns by value rather than reference. Can operators return by reference? Thanks.
...
CPos a(...), b(...), c(...);
...
CPos &CPos::Add(const CPos& l, const CPos& r) {
x = l.x + r.x;
y = l.y + r.y;
z = l.z + r.z;
return *this;
}
//assign a + b to C
c.Add(a, b);
now if I make an auxiliary operator for this, I do something along the lines of:
const CPos operator+ (const CPos &l, const CPos &r) {
return CPos().Add(l, r);
}
//so the user has the option of doing:
a = b + c; //slow
a = b + 1; //slow
a = 1 + b; //slow works because of NON-member operator
a.(b,c); //fastest