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

  • Home
  • SEARCH
  • 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 8286925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:54:21+00:00 2026-06-08T11:54:21+00:00

I have two questions about overloading. 1- Why sometimes do make overloading operators non-member

  • 0

I have two questions about overloading.

1- Why sometimes do make overloading operators non-member functions?

friend Class operator-(const Class &rhs);

2- What’s the difference between

Class operator+(const Class &c1, const Class &c2);

and

Class operator+(const Class &rhs);

if I want to add two objects C3 = C1 + C2?

Any help is appreciated…

  • 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-08T11:54:22+00:00Added an answer on June 8, 2026 at 11:54 am

    If you overload a binary operator as a member function, it ends up asymmetrical: the left operand must be the exact type for which the operator is overloaded, but the right operand can be anything that can be converted to the correct type.

    If you overload the operator with a non-member function, then both operands can be converted to get the correct type.

    What you have as your second point looks like a concrete example of the same point, not really anything separate at all. Here’s a concrete example of what I’m talking about:

    class Integer {
        int val;
    public:
        Integer(int i) : val(i) {}
        operator int() { return val; }
    
        // Integer operator+(Integer const &other) const { return Integer(val + other.val); }
    
        friend Integer operator+(Integer const &a, Integer const &b) { 
            return Integer(a.val + b.val);
        }
    };
    
    
    int main() { 
        Integer x(1);
    
        Integer y = x + 2; // works with either operator overload because x is already an Integer
    
        Integer z = 2 + x; // works with global overload, but not member overload because 2 isn't an Integer, but can be converted to an Integer.
        return 0;
    }
    

    Also note that even though the definition of the friend function is inside the class definition for Integer, the fact that it’s declared as a friend means it’s not a member function — declaring it as friend makes it a global function, not a member.

    Bottom line: such overloads should usually be done as free functions, not member functions. Providing the user with an operator that works correctly (drastically) outweighs theoretical considerations like “more object oriented”. When necessary, such as when the implementation of the operator needs to be virtual, you can do a two-step version, where you provide a (possibly virtual) member function that does the real work, but the overload itself is a free function that invokes that member function on its left operand. One fairly common example of this is overloading operator<< for a hierarchy:

    class base { 
        int x;
    public:
        std::ostream &write(std::ostream &os) const { 
            return os << x;
        }
    };
    
    class derived : public base { 
        int y;
    public:
        std::ostream &write(std::ostream &os) const { 
            return (base::write(os) << y);
        }
    };
    
    std::ostream &operator<<(std::ostream &os, base const &b) {
        return b.write(os);
    }
    

    This supports both polymorphic implementation (and access to a base class’ protected members, if necessary) without giving up the normal characteristics of the operator to get it.

    The primary exceptions to overloading binary operators as free functions are assignment operators (operator=, operator+=, operator-=, operator*= and so on). A conversion would produce a temporary object, which you can’t assign to anyway, so for this particular case, the overload should be (must be, as a matter of fact) a member function instead.

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

Sidebar

Related Questions

I have two questions about axis labels: How do I make a label at
I have two questions about static + const members and static classes. One: Why
I have two questions about java.awt.Shape . Suppose I have two Shape s, shape1
So I have two questions about HashMap s in Java: What is the correct
I have two noobish questions about Flash, Actionscript, Flex etc. 1) With these technologies
I'm building this website and I have basically two questions about this page: http://sites.publishyours.com.br/silviamecozzi/pt/obra/deserto_das_palmas.php
So basically, I have two fast questions about kohana urls. 1) With default .htaccess
I have two questions about using a BackgroundWorker: 1)Lets say you have Function A
I have two questions about Google's dense_hash_map , which can be used instead of
I have a two questions about authentication involving CouchDB. The questions are about a

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.