Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 885581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:55:59+00:00 2026-05-15T12:55:59+00:00

I know I can answer this question easily for myself by generatin the code

  • 0

I know I can answer this question easily for myself by generatin the code and see if it compiles. But since I couldn’t find a similar question, I thought it’s knowledge worth sharing.
Say I am overloading the + operator for MyClass. Can I overload it multiple times. Different overload 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 that I am building a class that I want to be as optimized as possible. Performance is the biggest concern for me here. So casting and using switch case inside the operator + overloaded function is not an option. I f you’ll notice, I made both the overloads inline. Let’s assume for a second that the compiler indeed inlines my overloads, then it is predetermined at compile time which code will run, and I save the call to a function (by inlining) + a complicated switch case scenario (in reality, there will be 5+ overloads for + operator), but am still able to write easily read code using basic arithmetic operators.
So, will I get the desired behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:55:59+00:00Added an answer on May 15, 2026 at 12:55 pm

    The canonical form of implementing operator+() is a free function based on operator+=(), which your users will expect when you have +. += changes its left-hand argument and should thus be a member. The + treats its arguments symmetrically, and should thus be 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 member functions defined with their class’ definition are implicitly inline. Also note, that within MyClass, the prefix MyClass:: is either not needed or even wrong.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I've seen the answer somewhere to this question but I just can't
I know this is somewhat subjective, but I can't find an honest answer anywhere.
Sorry that I can't find the answer through google - But I know this
I know this is the type of question that I can easily find all
I can surely answer to this question by myself writing a dummy test but
I can't find a straightforward yes or no answer to this! I know I
I know this is probably a common and easily answered question, but I'm a
I think I already know the answer to this question is No, but I
I hope someone can answer this question. How does the UuidCreateSequential method in the
I found Difference between […]Async and Begin[…] .net asynchronous APIs question but this answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.