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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:29:35+00:00 2026-06-12T09:29:35+00:00

Continuing from the previous question I’d like to ask why the friend form of

  • 0

Continuing from the previous question I’d like to ask why the “friend” form of addition in a C++ operator override is preferred

To summarize:

for the addition operator override there are two ways to do it:

int operator+(Object& e);
friend int operator+(Object& left, Object& right);

why is that the second (friend) form is preferred? What are the advantages?

  • 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-12T09:29:36+00:00Added an answer on June 12, 2026 at 9:29 am

    The non-member version (friend or otherwise) is preferred because it can support implicit conversions on both the left and right side of the operator.

    Given a type that is implicitly convertible to Object:

    struct Widget
    {
      operator Object() const;
    };
    

    Only the non-member version can be called if an instance of Widget appears on the left-hand side:

    Widget w;
    Object o;
    
    o + w; // can call Object::operator+( Object & ) since left-hand side is Object
    w + o; // can only call operator+( Object &, Object & )
    

    In response to your comment:

    By defining the conversion operator in Widget, we are notifying the compiler that instances of Widget can be automatically converted to instances of Object.

    Widget w;
    Object o = w;  // conversion
    

    In the expression o + w, the compiler calls Object::operator+( Object & ) with an argument generated by converting w to an Object. So the result is the same as writing o + w.operator Object().

    But in the expression w + o, the compiler looks for Widget::operator+ (which doesn’t exist) or a non-member operator+( Widget, Object ). The latter can be called by converting w to an Object as above.

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

Sidebar

Related Questions

Continuing from my previous question , is there a comprehensive document that lists all
I am continuing on from a previous question relating to loading instances of plugins
Continuing from a previous question , I keep searching for the optimal way to
Continuing my previous question Why I cannot derive from long? I found an interesting
I am continuing from my previous question I need a way to export my
Continuing from my previous question which was solved by @Oleg. The solution seems to
Continuing from my last question , I'd like to know how can I bind
Continuing from this question : When I am trying to do fopen on Windows,
Continuing a previous question I asked here , I now need to move to
My question stems from MVC's SelectList (and previous generations). Basically the class takes in

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.