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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:10:52+00:00 2026-06-08T05:10:52+00:00

So, after reading some SO questions and answers, i still doesn’t understand why use

  • 0

So, after reading some SO questions and answers, i still doesn’t understand why use

friend bool operator==( BaseClass const &left, BaseClass const &right )

instead of

bool operator==( BaseClass const &right )

right now I have something like this http://pastebin.com/pKsTabC0 (Fixed) – and it seems to work fine. But maybe I’m missing something? Any Suggestions?

Update 1

Ok i changed the source to make it work right http://ideone.com/fIAmB. Removed unnecessary virtual and added const. Still i dont understand why to use friends…

  • 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-08T05:10:54+00:00Added an answer on June 8, 2026 at 5:10 am

    Your derived function doesn’t have the same signature as the parent operator, so it hides the parent comparison rather than overriding it. This means that you can’t make use of the virtual-ness anyway since the static type of the left hand argument determines the function that’s called.

    That’s why the normal method for virtual comparison is a non-member equality operator that dispatches to a virtual comparison function in the base class.

    Do consider your specific need for virtual comparison though as it may be a design smell that there’s an alternate design you could use instead.

    Also note that member comparison (equality in this case) operators should usually be const.

    EDIT: It seems that you may only care about comparison based on the static type of the left hand argument, which should be an easier problem. In this case your code handles all cases except where the left hand argument is a type implicitly converts to a Base or Derived by some mechanism other than inheritance (conversion operator or converting constructor). If you don’t care about those cases, then the member equality is fine.

    As one final note, if the comparison could be done entirely through the public interface, we (almost) always prefer non-member, non-friend functions regardless of whether or not it’s an operator.

    EDIT2 (A really quick overview of non-member, non-friend):

    For example suppose your class has a public key method, and if the keys of two instances are equal you want to call the objects equal. Then, without using friends or a member equality operator, you can write your equality standalone:

    bool operator==(const MyType& left, const MyType& right)
    {
        return left.key() == right.key();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading some questions/answers about activity and process lifecycle. I suppose I understand the
After reading a couple of answers and comments on some SQL questions here, and
After reading some questions and answers about enum I don't find it really useful...
I came across couple of questions about OCL expressions. After reading some university slides
After reading some material on this subject I'm still not sure what the difference
I'm trying to understand how ID3 tags work, so, after reading some documentation, I
After reading some examples on stackoverflow, and following some of the answers for my
After searching a lot, reading every tutorials I've found and asking some questions here,
After reading this question, I need to clear up some things. IQueryable<Customer> custs =
After reading some articles about XSS I have incorporated HTMLPurifier into my zend framework

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.