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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:48:48+00:00 2026-05-23T01:48:48+00:00

In some books and often around the internet I see recommendations like operator== should

  • 0

In some books and often around the internet I see recommendations like “operator== should be declared as friend”.

How should I understand when an operator must be declared as friend and when it should be declared as member? What are the operators that will most often need to be declared as friends besides == and <<?

  • 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-23T01:48:49+00:00Added an answer on May 23, 2026 at 1:48 am

    This really depends on whether a class is going to be on the left- or right-hand side of the call to operator== (or other operator). If a class is going to be on the right-hand side of the expression—and does not provide an implicit conversion to a type that can be compared with the left-hand side—you need to implement operator== as a separate function or as a friend of the class. If the operator needs to access private class data, it must be declared as a friend.

    For example,

    class Message {
        std::string content;
    public:
        Message(const std::string& str);
        bool operator==(const std::string& rhs) const;
    };
    

    allows you to compare a message to a string

    Message message("Test");
    std::string msg("Test");
    if (message == msg) {
        // do stuff...
    }
    

    but not the other way around

        if (msg == message) { // this won't compile
    

    You need to declare a friend operator== inside the class

    class Message {
        std::string content;
    public:
        Message(const std::string& str);
        bool operator==(const std::string& rhs) const;
        friend bool operator==(const std::string& lhs, const Message& rhs);
    };
    

    or declare an implicit conversion operator to the appropriate type

    class Message {
        std::string content;
    public:
        Message(const std::string& str);
        bool operator==(const std::string& rhs) const;
        operator std::string() const;
    };
    

    or declare a separate function, which doesn’t need to be a friend if it doesn’t access private class data

    bool operator==(const std::string& lhs, const Message& rhs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for some useful books for a beginner who wants to better understand
I see some web pages are centered (like stackoverflow.com) in the middle while others
I have been learning C++ with some books from school that are from the
I have read through several reviews on Amazon and some books seem outdated. I
Preferred languages : C/C++, Java, and Ruby. I am looking for some helpful books/tutorials
Possible Duplicate: String vs StringBuilder I just revisited some of the books that I
Could you give some guidance for good resources (books, online tutorials) about embedded assembly
I remember reading in some Java book about any operator other than 'instanceof' for
can you recommend some good ASP.NET tutorials or a good book? Should I jump
I've read some books on creating stateless websites, I've read some about stateful client

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.