Overloading multiple C ++ statements for the same statement

I know that I can easily answer this question for myself by indicating the code and seeing if it is compiled. But since I could not find a similar question, I thought this knowledge was worth sharing. Let's say I overload the + operator for MyClass. Can I reload it several times. Different overloads for different types. Like this:

class MyClass{ ... inline const MyClass operator+(const MyClass &addend) const { cout<<"Adding MyClass+MyClass"<<endl; ...//Code for adding MyClass with MyClass } inline const MyClass operator+(const int &addend) const { cout<<"Adding MyClass+int"<<endl; ...//Code for adding MyClass with int } ... }; int main(){ MyClass c1; MyClass c2; MyClass c3 = c1 + c2; MyClass c4 = c1 + 5; } /*Output should be: Adding MyClass+MyClass Adding MyClass+in*/ 

The reason I want to do this is because I am creating a class that needs to be optimized as much as possible. The biggest concern for me here. So casting and using the switch housing inside the operator + overloaded function is not an option. I notice I did and overload built-in. Suppose for a second that the compiler really sets my overloads, then it is predefined at compile time, the code of which will be launched, and I save the function call (by inlining) + a complex script of the switching script (in reality, there will be 5+ overloads for the + operator), but I can still write easily readable code using basic arithmetic operators. So, will I get the desired behavior?

+7
c ++ operators operator-overloading
source share
3 answers

The canonical implementation form of operator+() is a free function based on operator+=() that your users expect if you have + . += modifies the left argument and therefore must be a member. + considers its arguments symmetrically and, therefore, is a free function.

Something like this should do:

 //Beware, brain-compiled code ahead! class MyClass { public: MyClass& operator+=(const MyClass &rhs) const { // code for adding MyClass to MyClass return *this; } MyClass& operator+=(int rhs) const { // code for adding int to MyClass return *this; } }; inline MyClass operator+(MyClass lhs, const MyClass& rhs) { lhs += rhs; return lhs; } inline MyClass operator+(MyClass lhs, int rhs) { lhs += rhs; return lhs; } // maybe you need this one, too inline MyClass operator+(int lhs, const MyClass& rhs) { return rhs + lhs; // addition should be commutative } 

(Note that the member functions defined with the definition of their class are implicitly inline . Also note: inside MyClass prefix MyClass:: either not needed, or even wrong.)

+8
source share

Yes.


These operator functions are just ordinary functions with the special names operator@ . There are no restrictions that they cannot be overloaded. In fact, the << operator used by iostream is a multi-overload operator.

+9
source share

Yes, you can overload such operators. But I'm not sure what you mean by "switch case". You can live with one overload if you have a conversion constructor

 class MyClass{ ... // code for creating a MyClass out of an int MyClass(int n) { ... } ... inline const MyClass MyClass::operator+(const MyClass &addend) const { cout<<"Adding MyClass+MyClass"<<endl; ...//Code for adding MyClass with MyClass } ... }; 

No switch is needed at all. It is eligible if "MyClass" logically represents a number.

Please note that you must overload these operators with non-member functions. In your code, 5 + c1 will not work, because there is no operator that accepts int as the left side. Following will work

 inline const MyClass operator+(const MyClass &lhs, const MyClass &rhs) { // ... } 

Now, if you save the conversion constructor, you can add an int on either side with minimal code overhead.

+2
source share

All Articles