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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:39:57+00:00 2026-05-12T20:39:57+00:00

In my homework, I have to design a class Message; among other attributes, it

  • 0

In my homework, I have to design a class Message; among other attributes, it has attribute “priority” (main goal is to implement priority queue).

As in container I must check if one object is greater than other, I have overloaded operator ‘>’. Now, I have a few general questions about it…

Question one:

If I overload operator ‘>’, should I overload operator ‘<‘ for arguments (const Message&, const Message&)?

My opinion is that overloading both > and < and using it in code will generate an error:

if(message1 > message2)
   { ... }

(Does the following code calls operator > for message1 object, or operator < message2 object?)

But, what if I use operator like this:

if(message1 < message2)
   { ... }

?

operator> is declared as friend function:

friend bool operator>(const Message& m1, const Message& m2)

Does it need to be declared as member function?

Thank you.

  • 1 1 Answer
  • 1 View
  • 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-05-12T20:39:58+00:00Added an answer on May 12, 2026 at 8:39 pm

    If I overload operator ‘>’, should I overload operator ‘<‘ for argumenst (const Message&, const Message&)?

    Yes. In fact, it’s convention in most code to prefer the usage of < over > (don’t ask me why, probably historical). But more generally, always overload the complete set of related operators; in your case, this would probably also be ==, !=, <= and >=.

    (Does the following code calls operator > for message1 object, or operator < message2 object?)

    It always calls what it finds in the code. For the C++ compiler, there’s absolutely no connection between > and <. For us, they look similar but the compiler sees two completely different, unrelated symbols. So there’s no ambiguity: the compiler calls what it sees.

    Does it need to be declared as member function?

    No. In fact, it’s best not declared as a member function. Declaring it as a member function means that the first argument (i.e. the left-hand side of the expression) must really be a Message object, rather than an object that is implicitly convertible to a Message.

    To understand this, consider the following case:

    struct RealFraction {
        RealFraction(int x) { this.num = x; this.den = 1; }
        RealFraction(int num, int den) { normalize(num, den); }
        // Rest of code omitted.
    
        bool operator <(RealFraction const& rhs) {
            return num * rhs.den < den * rhs.num;
        }
    };
    

    Now you can write the following comparison:

    int x = 1;
    RealFraction y = 2;
    if (y < x) …
    

    but you cannot write the following:

    if (x < y) …
    

    although there exists an implicit conversion from int to RealFraction (using the first constructor).

    If, on the other hand, you had used a non-member function to implement the operator, both comparisons would work because C++ would know to call an implicit constructor on the first argument.

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

Sidebar

Related Questions

I have the following assignment for homework. Requirements Design a class called TokenGiver with
Hi I have a homework assignment where I need to implement an intersection of
For my algorithm design class homework came this brain teaser: Given a list of
I have homework and I'm supposed to draw a class diagram AND data model.
I have this homework question: Assume a Database table has 0 records at time
This is what I want to accomplish for homework: Design and implement a program
As a homework assignment for my introductory programming course I have to design and
I have to do a homework where i have to implement user registration and
I have a homework that asks me to do the following: Design a template
I have a homework problem for my C++ class and the problem wants us

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.