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 8491559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:27:40+00:00 2026-06-10T22:27:40+00:00

In C#, to overload an operator such as ‘+’, ‘-‘ etc, I have to

  • 0

In C#, to overload an operator such as ‘+’, ‘-‘ etc, I have to make the function a static member of the class:

class MyType
{
   /*...*/

   public static MyType operator+ (MyType a, MyType b)
   {
       MyType ret;
       /* do something*/
       return ret;
   }
}

As far as I know, in C++ this is how I can overload an operator:

class MyType
{
   /*...*/

public:
   MyType operator+ (MyType b) // *this is the first operand
   {
       MyType ret;
       /* do something*/
       return ret;
   }
};

The problem is that *this is the first operand, so the first operand must be of type MyType. For example, if I want to add MyType to an integer:

MyType a, b;
b = a + 1;  // Valid
b = 1 + a;  // Error

In C#, I can overload the ‘+’ operator for each case.

My question is: can I do in C++ the same as in C#, use static operators? As far as I know, there is one way to do that, with friend operators, but they are lost when inheriting the function.

  • 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-06-10T22:27:41+00:00Added an answer on June 10, 2026 at 10:27 pm

    Make the operator+ overload with int on the left hand side a free function instead of a member function of MyType:

    class MyType
    {
      ...
    
      // MyType + int can be a member function because MyType
      // is the type of the sum's left hand side
      MyType operator+(int rhs) const;
    };
    
    // int + MyType needs to be a free function because
    // int is the type of the sum's left hand side
    MyType operator+(int lhs, const MyType &rhs);
    

    Another common idiom is to make the overloads a friend of the class of interest. Now you can implement both cases in the same way:

    class MyType
    {
      ...
    
      friend MyType operator+(int lhs, const MyType &rhs)
      {
        // get access to MyType's private members here
        // to implement the sum operation
        ...
      }
    
      friend MyType operator+(const MyType &lhs, int rhs)
      {
        // you can also implement the symmetric case
        // of int on the right hand side here
        ...
      }
    };
    

    Note that even though the operator+ overloads look like member functions in the 2nd example, they are actually free functions that live in the global scope due to their declaration as friends of MyType.

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

Sidebar

Related Questions

I am trying to overload operator<< as a member function. It works if simply
Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
Is there a way to overload the << operator, as a class member, to
why The assignment operator must be a non-static member function operator(), operator[], and operator->
I mean, I was trying to overload the operator<< inside the class like this
I try to do something like class base { public: virtual double operator() (double
When you overload an operator, such as operator + , the compiled CIL looks
§27.7.3.9 defines the following overload for operator<< : template <class charT, class traits, class
I want to overload the assignment operator for types like int, long etc. That
The assignment operator can be overloaded using a member function but not a non-member

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.