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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:52:52+00:00 2026-05-11T05:52:52+00:00

I have a base class which implements the == operator. I want to write

  • 0

I have a base class which implements the == operator. I want to write another class, inheriting the base class, and which should reimplement the == operator.

Here is some sample code :

#include <iostream> #include <string>  class Person { public:   Person(std::string Name) { m_Name = Name; };    bool operator==(const Person& rPerson)   {     return m_Name == rPerson.m_Name;   }  private:   std::string m_Name; };  class Employee : public Person { public:   Employee(std::string Name, int Id) : Person(Name) { m_Id = Id; };    bool operator==(const Employee& rEmployee)   {      return (Person::operator==(rEmployee)) && (m_Id == rEmployee.m_Id);   }  private:   int m_Id; };  void main() {   Employee* pEmployee1 = new Employee('Foo' , 1);   Employee* pEmployee2 = new Employee('Foo' , 2);    if (*pEmployee1 == *pEmployee2)   {     std::cout << 'same employee\n';   }   else   {     std::cout << 'different employee\n';   }    Person* pPerson1 = pEmployee1;   Person* pPerson2 = pEmployee2;    if (*pPerson1 == *pPerson2)   {     std::cout << 'same person\n';   }   else   {     std::cout << 'different person\n';   } } 

This sample code give the following result :

different employee same person 

Where I would like, even when handling Person* pointers, to make sure they are different.

How am I supposed to solve this problem ?

Thanks !

  • 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. 2026-05-11T05:52:53+00:00Added an answer on May 11, 2026 at 5:52 am

    What you want to do is essentiall "virtualize" the comparison operator.

    Since operators cannot be virtual (operators can be virtual), you will need to delegate it to something else. Here’s one possible solution.

    class Person {    public:       /* ... */       bool operator==(const Person& rhs)       {          return m_Name == rPerson.m_Name && this->doCompare(rhs);       }    private:       virtual bool doCompare() = 0;    }; } class Employee : public Person {    /* ... */    private:       virtual bool doCompare(const Person& rhs)       {          bool bRetval = false;          const Employee* pRHSEmployee = dynamic_cast<const Employee*>(&rhs);          if (pEmployee)          {             bRetval = m_Id == pRHSEmployee->m_Id          }          return bRetval;       } }; 

    The question didn’t make clear whether Person needs to be a concrete class. If so, you can make it not pure-virtual, and implement it to return true.

    This also uses RTTI, which you may or may not be happy with.

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

Sidebar

Ask A Question

Stats

  • Questions 170k
  • Answers 170k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is some good info here: Create a SQL Server… May 12, 2026 at 2:06 pm
  • Editorial Team
    Editorial Team added an answer SQL 2008 will allow you to run a Profiler trace… May 12, 2026 at 2:06 pm
  • Editorial Team
    Editorial Team added an answer From the db point of view, you should load all… May 12, 2026 at 2:06 pm

Related Questions

I have implemented the IEquatable interface in a class with the following code. public
Lets pretend that for some reason I want to create a custom control that
In general reflection in a base class can be put to some good and
I am trying to become a good programming citizen through learning more about Dependency

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.