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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:03:18+00:00 2026-06-13T07:03:18+00:00

Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? I

  • 0

Possible Duplicate:
What’s the right way to overload operator== for a class hierarchy?

I have a base class and several derived classes like in the following code:

class Base
{
public:
    friend bool operator==(const Base&, const Base&);
    virtual ~Base(){}

private:
    virtual bool equals(const Base& other) const = 0;
};

bool operator==(const Base& lhs, const Base& rhs)
{
    return lhs.equals(rhs);
}

class A : public Base
{
public:
    A(int x) : x_(x){}

private:
    virtual bool equals(const Base& other) const;
    int x_;
};

bool A::equals(const Base& other) const
{
    const A* pA = dynamic_cast<const A*>(&other);
    if(!pA) return false;
    return x_ == pA->x_;
}

class B : public Base
{
public:
    B(double y) : y_(y){}

private:
    virtual bool equals(const Base& other) const;
    double y_;
};

bool B::equals(const Base& other) const
{
    const B* pB = dynamic_cast<const B*>(&other);
    if(!pB) return false;
    return y_ == pB->y_;
}

To be able to compare two derived classes I want to have operator==. The question is how to achieve this in an object oriented way (e.g. respecting encapsulation, maintainability and extensibility). Are there some recommended patterns to do that? Is there an alternative to the above approach avoiding dynamic_cast?

  • 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-13T07:03:19+00:00Added an answer on June 13, 2026 at 7:03 am

    Your way is not perfect. Consider next class deriving from A:

    class AA : public A
    {
    public:
        AA(int x, int y) : A(x), y_(y) {}
    
    private:
        virtual bool equals(const Base& other) const;
        int y_;
    };
    
    bool AA::equals(const Base& other) const
    {
        const AA* pAA = dynamic_cast<const AA*>(&other);
        if(!pAA) return false;
        return A::equals(other) && y_ == pAA->y_;
    }
    

    then your operatator == breaks fundamental rule, it is not symmetrical relation:

       A a(1); 
       AA aa(1,1); 
       assert(a == aa);
       assert(!(aa == a));
    

    Short fix would be to use typeid:

    bool operator==(const Base& lhs, const Base& rhs)
    {
        return typeid(lhs) == typeid(rhs) && lhs.equals(rhs);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In
Possible Duplicate: When are two enums equal in C#? I have the following classes
Possible Duplicate: What’s the Right Way to use the rand() Function in C++? When
Possible Duplicate: C# member variable initialization; best practice? Which is the right way to
Possible Duplicate: PHP method chaining? I occasionally see some php applications use classes like
Possible Duplicate: ViewPager and fragments — what's the right way to store fragment's state?
Possible Duplicate: What is the right way to typedef a type and the same
Possible Duplicate: What is the right way to use spring MVC with Hibernate in
Possible Duplicate: What is the right way to check for a null string in
Possible Duplicate: Loading multiple versions of the same class What is the best way

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.