I get how this operator overloading works here in this code.....
class operatorOver { public: int a, b, c; }; operatorOver operator+(const operatorOver &a, const operatorOver &b) { operatorOver aa; aa.a = b.a + a.a; return aa; } int main() { operatorOver aaa, bbb, ccc; aaa.a = 100; bbb.a = 200; ccc = aaa + bbb; cout << ccc.a << endl; system("pause"); }; but this version I don't understand how this one works here....
class operatorOver { public: operatorOver operator+(const operatorOver &a) { operatorOver aa; aa.a = (*this).a + a.a; return aa; } int a, b, c; }; int main() { operatorOver aaa, bbb, ccc; aaa.a = 100; bbb.a = 200; ccc = aaa + bbb; cout << ccc.a << endl; system("pause"); }; the 1st one I showed, I'm assuming the code of the operator overloading here is taking into 2 object classes in order for it to work...
but how come the 2nd example it's showing that I don't need to create another object class in it's parameters but still work... when you look in main() you see that there are 2 object classes still being passed in.... I'm lost.
a + bif a isn't an operatorOver and operatorOver has a conversion constructor from the type of a to operatorOver, it will be called, whereas use of a member function will not do that. Sidenote: start your classes with a capital, unless you have a good reason not to. It is the habit for most C++ developers if they use camel case. So OperatorOver rather than operatorOver.