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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:18:26+00:00 2026-05-26T15:18:26+00:00

For example: in a C++ header file, if I defined a struct Record and

  • 0

For example: in a C++ header file, if I defined a struct Record and I would like to use it for possible sorting so that I want to overload the less operator. Here are three ways I noticed in various code. I roughly noticed that: if I’m going to put Record into a std::set, map, priority_queue, … containers, the version 2 works (probably version 3 as well); if I’m going to save Record into a vector<Record> v and then call make_heap(v.begin(), v.end()) etc.. then only version 1 works.

  struct Record
  {
      char c;
      int num;

      //version 1
      bool operator <(const Record& rhs)
      {
         return this->num>rhs.num;
      }

      //version 2
      friend bool operator <(const Record& lhs, const Record& rhs) //friend claim has to be here
      {
         return lhs->num>rhs->num;
      }
  };

in the same header file for example:

      //version 3
      inline bool operator <(const Record& lhs, const Record& rhs)
      {
         return lhs->num>rhs->num;
      }

Basically, I would like to throw the questions here to see if someone could come up with some summary what’s the differences among these three methods and what are the right places for each version?

  • 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-05-26T15:18:26+00:00Added an answer on May 26, 2026 at 3:18 pm

    They are essentially the same, other than the first being non-const and allowing you to modify itself.

    I prefer the second for 2 reasons:

    1. It doesn’t have to be a friend.
    2. lhs does not have to be a Record
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have C header file containing the following type definition: // example.h typedef struct
Is it possible to use multiple header types in one document? For example: header(Content-type:
My basic problem is that I want to use some structs and functions defined
I have a few properties defined in my header file like so @property (assign)
I have a c++ header file containing a class. I want to use this
Take this file as example,there are many non-extern structures like: struct list_head source_list; How
For example: Base class header file has: enum FOO { FOO_A, FOO_B, FOO_C, FOO_USERSTART
Example (native GIF header): GIF89a (47 49 46 38 39 61) I want to
I have a structure Defined in the Header file for a class i am
I have a class level int defined in my header file. In the .m

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.