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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:37:05+00:00 2026-06-13T11:37:05+00:00

I have a class called user which has a lname field. Is this the

  • 0

I have a class called user which has a lname field. Is this the right way to overload the “<” operator?

bool User::operator<(const User& other)
{
    std::cout << "< operator was called" << std::endl;
    if (this != &other)
    {
        if (lname.compare(other.lname) == 0)
        {
            return true;
        }
    }
    return false;
}

I am trying to use this in a more complicated set of things and it is failing – just want to make sure this much is right.

  • 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-13T11:37:06+00:00Added an answer on June 13, 2026 at 11:37 am

    As others have pointed out, your operator< doesn’t allow the left side to be const. Changing the function signature to

    bool User::operator<(const User& other) const
    

    is an improvement. But I would actually recommend making it a non-member function instead:

    class User {
    public:
        friend bool operator<(const User& u1, const User& u2);
        // ...
    };
    
    bool operator<(const User& u1, const User& u2)
    {
        // ...
    }
    

    For one thing, it’s a little more legible in my opinion.

    But also, it sometimes makes a technical difference. With a non-member function, the expression a < b attempts implicit conversions on both a and b to see if your operator< is a viable overload. But with a member function, implicit conversions can apply to b, but not to a: a must be of type User or a derived type. This can lead to surprising situations where a < b compiles but b < a doesn’t.

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

Sidebar

Related Questions

I have a class User with one field called birthDate which is a java.sql.Date
I have a class called User which has 2 properties : login/password. I am
My situation is essentially this: I have a class called Foo which has dependencies
I have a class called Flamethrower which naturally has its own ammunition that is
I have a Door class object I made, which has an ISceneNode member called
I have a class called ListViewController which has a UITableView attached to it and
I have an Abstract class called Function which has 2 inheriting classes (actually 6,
I have a user control which contains a textbox. I have a class called
I have created a Django model called Person, which has got a 'user' ForeignKey
I have a class called UserInfo that contains details about a given user. There

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.