3

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.

1
  • By the way, I'd prefer the first way (non-member function) since in that case in a + b if 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. Commented Aug 19, 2017 at 15:17

2 Answers 2

3

Some of the binary operators, such as +, can be overloaded as member functions as well as non-member functions.

When + is overloaded as a member function, the function needs to be declared with one argument. When the operator is used as:

a + b 

the call is resolved as

a.operator+(b); 

When it is overloaded as a non-member function, the function needs to be declared with two arguments. When the operator is used as:

a + b 

the call is resolved as

operator+(a, b); 

Further reading: http://en.cppreference.com/w/cpp/language/operators

Sign up to request clarification or add additional context in comments.

3 Comments

ohhhhhhhhhhh i get it now, ty! so let me get this straight... if the overload operator is defined within the class structure, it will become 'a.operator+(b)', but when the overload operator is defined outside the class structure it will become 'operator+(a, b)'....
@thundergawd, you are welcome. Glad I was able to help.
still got a question... why does it require a (*this) in order for the 1st example to work? is (*this) == aaa? why is this the case? EDIT - oh nvm, i just figured it out, ty very much!
3

In the second example, two object are passed. There's a and there is also this. The object passed as this is the left side of the operation.

Also note that your member operator+ should be const, since it doesn't mutate any data members of this.

Your member operator also invoke undefined behavior, since you are using a unassigned value:

// The member function operatorOver operator+(const operatorOver &a) { operatorOver aa; // What is the value of aa.a? Undefined behavior! aa.a = aa.a + a.a; return aa; } 

To be equivalent to you non-member function, it should be this implementation:

// The member function operatorOver operator+(const operatorOver &a) const { operatorOver aa; // Member `a`, could be written `this->a` aa.a = a + a.a; return aa; } 

1 Comment

yea i just updated the correction on my code lol i forgot to put (*this) in, before i had it but changed it up to demonstrate the first ex. and went back to fix up the 2nd ex. i forgot to put it correctly.....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.