Hello everyone just a general question on different syntax on operator overloading and which are better in certain situations if indeed there is any difference.
Ive only really been using operator overloads for 8 months and have come across a few different methods. The most popular doing something like this:
bool operator == (const MyClass &mc)
{
return (variable == mc.variable);
}
and when discussing something on gamedev i remember a very helpful poster who said it helps to have 2 operator overloads of the same operator one including const which i think was written like this :
bool operator == (const MyClass &mc) const
{
return (variable == mc.variable);
}
and sometimes like this:
const bool operator == (const myClass &mc) const //(which to me seems like it could just do with the const at the front or the beginning)
{
return (variable == mc.variable);
}
and i found that the first method didnt work when i was working in VS 2005 at uni implementing a class that was later used in a list/vector/map that then used a find function from the algorithm header. Instead i think the third method was the only one that worked whereas here at home on vs 2008 it doesn't seem to be making a difference.
The final method is the one being taught to us at university which although works in what seems all situations, i really don't like the syntax
friend bool operator < (const MyClass &lhs, const MyClass &rhs);
//with the body being defined else where e.g.
bool operator < (const MyClass &lhs, const MyClass &rhs)
{
if (lhs.name != rhs.name)
return (lhs.name < rhs.name);
else
return (lhs.ptype < rhs.ptype);
}
Is this actually the better way of doing it? or is there no real difference? I personally prefer the second method for comparison overloads as it never seems to give me any trouble. Also to me it seems like abuse of the friend operator to make the function global, which seems to me like bad practice. Then again ive never really thought the friend operator should be used unless really really necessary as to me it in certain situation it defeats the point of encapsulation, ive used a friend class once and 2bh there was probably a better way round that.
However when i do assignments or multiplications the only method i've come across that i've used is:
const MyClass &operator=(const MyClass &myclass)
{
variable = myclass.variable;
return *this;
}
//and
const MyClass &operator+(const MyClass &myclass) const
{
return(MyClass ( variable + myclass.variable)); //with coresponding constructor
}
//i remember trying something more like and having issues (but that was a while ago, maybe i was just being stupid)
const MyClass &operator+(const MyClass &MyClass ) const
{
variable += myclass.variable;
return *this;
}
i remember trying some other methods and having problems but i can't remember what they where and so have been sticking to the above.
Any advice or pointers on the subject and points raised would be much appreciated, Thank you, Chris
[Edited by - ChrisPepper1989 on November 25, 2008 3:53:39 PM]