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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:43:13+00:00 2026-06-05T10:43:13+00:00

I was reading the C++ FAQ . There I found a point in the

  • 0

I was reading the C++ FAQ. There I found a point in the guideline for operator overloading uses:

If you provide constructive operators, they should allow promotion of the left-hand operand (at least in the case where the class has a single-parameter ctor that is not marked with the explicit keyword). For example, if your class Fraction supports promotion from int to Fraction (via the non-explicit ctor Fraction::Fraction(int)), and if you allow x – y for two Fraction objects, you should also allow 42 – y. In practice that simply means that your operator-() should not be a member function of Fraction. Typically you will make it a friend, if for no other reason than to force it into the public: part of the class, but even if it is not a friend, it should not be a member.

Why has the author written that operator-() should not be member function?

What are the bad consequences if I make operator-() as member function and what are other consequences?

  • 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-05T10:43:15+00:00Added an answer on June 5, 2026 at 10:43 am

    Here is Fraction with the operator as a member function:

    class Fraction
    {
        Fraction(int){...}
    
        Fraction operator -( Fraction const& right ) const { ... }
    };
    

    With it, this is valid code:

    Fraction x;
    Fraction y = x - 42;
    

    and its equivalent to x.operator-( Fraction(42) ); but this is not:

    Fraction z = 42 - x;
    

    Because 42 has no member function operator - in it (of course, its not even a class).

    However, if you declare your operator as a free function instead, conversion operations apply to both of its arguments. So this

    Fraction z = 42 - x;
    

    turns into this

    Fraction z = Fraction(42) - x;
    

    which is equivalent to operator-( Fraction(42), x ).

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

Sidebar

Related Questions

I was reading Stroustrup's c++ FAQ and I noticed that, at one point, he
I am coming back from after reading this c-faq question I am totaly confused
I was reading WCF .NET 4 faq. I'm not sure with the answer for
Reading some questions here on SO about conversion operators and constructors got me thinking
Lately, I've been reading much about constructors from the well-received C++ FAQ . One
I'm reading a book C progaming faq's. Here is passage of the book Automatic
Reading through the CKEditor documentation , I see that they have an option to
Reading this site, I've found this : [The] line private static final Foo INSTANCE
I was reading the SQLite FAQ , and came upon this passage: Threads are
Reading the Redis documentation on Virtual Memory at: http://redis.io/topics/virtual-memory With regards to vm-max-threads they

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.