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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:13:23+00:00 2026-06-03T00:13:23+00:00

Possible Duplicate: What are the basic rules and idioms for operator overloading? Operator overloading

  • 0

Possible Duplicate:
What are the basic rules and idioms for operator overloading?
Operator overloading : member function vs. non-member function?

After many years of apparently abusing this construct somebody pointed out to me that this is bad practice:

class SomeClass
{
    ...
    bool operator<(const SomeClass& other) const;
};

whereas this is good practice:

class SomeClass
{
    ...
};
bool operator<(const SomeClass& a, const SomeClass& b);

But I can’t for the life of me figure out why and can’t find any docs on the difference. Can anyone point me in the right direction?

  • 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-03T00:13:25+00:00Added an answer on June 3, 2026 at 12:13 am

    The first thing is that there is no advantage in implementing the operator as a member function, and there can be advantages in doing it as a free function. In particular, a member function is not symmetric with respect to the types of the two operands, the left-hand-side (lhs) must be of the exact type of the class on which it is being called, while the right-hand-side (rhs) can use implicit conversions. In the case of free function operators, the same conversions can be applied to lhs and rhs.

    struct SomeClass {
        SomeClass( int value );
        bool operator<( SomeClass const & ) const;
    };
    bool operator>( SomeClass const &, SomeClass const & );
    int main() {
       SomeClass x( 10 );
       x < 100;             // Fine, lhs is SomeClass
       // 100 < x;          // Error, no operator< can take int as lhs and SomeClass
       x > 100;             // Fine
       100 > x;             // Also fine, lhs can take the same implicit conversions
    }
    

    Note that the main difference here are the implicit conversions, and that makes a difference only if your type can be implicitly converted from other types. If there are no implicit conversions into your type, then this point would be moot, but considering that there is no disadvantage in using a free function and there are advantages in some cases, I would use free functions whenever possible.

    I wrote some time ago about operator overloading here, you might find some suggestions there.

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

Sidebar

Related Questions

Possible Duplicate: How to write a basic swap function in Java Hi. I don't
Possible Duplicate: Collection initialization syntax in Visual Basic 2008? How is the following C#
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
Possible Duplicate: Workaround for basic syntax not being parsed I am trying to allow
Possible Duplicate: How do I overload the square-bracket operator in C#? Basically I want
Possible Duplicate: Python Ternary Operator If Python would support the (x ? a :
Possible Duplicate: Absolute path & Relative Path This is a very basic question, but
Possible Duplicate: List of ggplot2 options? I have a pretty basic question on ggplot2.

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.