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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:52:29+00:00 2026-06-16T15:52:29+00:00

using an interface (abstract class) on c++ i have the need to force any

  • 0

using an interface (abstract class) on c++ i have the need to force any class than inherits the interface to implement the operator ==.
consider this situation:

class IBase
{
   virtual void someFunc() const = 0; 
}

class CInheritClass : public IBase
{
   virtual void someFunc() const; 
   virtual bool operator== ( const CInheritClass& obj ) const;
}

void main()
{
  CInheritClass instance;
}

class CInheritClass must implement someFunc since it inherits Ibase, implementing virtual bool operator== ( const CInheritClass& obj ) const; is not mandatory.
i would like to modify IBase class in a way that any inheritor X will have to implement

virtual bool operator== ( const X& obj ) const

the following code will works:

template<class X>
class IBase
{
   virtual void someFunc() const = 0; 
   virtual bool operator== ( const X& obj ) const = 0;

}

class CInheritClass : public IBase<CInheritClass>
{
   virtual void someFunc() const; 
   virtual bool operator== ( const CInheritClass& obj ) const;
}

but i am after a solution that does not use templates cause every class that wishes to implement IBase must inherit IBase with itself as the template class class X : public IBase<X> and that is confusing and unclear to any future developer that might have a look on my code.
any idea ?

  • 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-16T15:52:31+00:00Added an answer on June 16, 2026 at 3:52 pm

    Use a pure virtual function declaration

    virtual bool operator== ( const IBase& obj ) const = 0;
    

    A example of implementation :

    bool CInheritClass ::operator==( const IBase& obj ) const
    {
         const CInheritClass *o = dynamic_cast<const CInheritClass*>(&obj);
         if (o == NULL) return false;
         // TODO
    }
    

    The input type should be the base class, if not you will not be able to do polymorphically call comparison on base objects

    For the idea of inherits from a template: warning, the base class is no longer the same between 2 derived class.
    => The purpose of the base class changed : With template you just share code, force user to implement function and prevent duplicate code between the 2 derived class

    It really depend what you are trying to do. It can be a good design, or not…

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

Sidebar

Related Questions

I have this base class having the following interface: abstract class Base { abstract
I have this classes interface UserInterface Then abstract class user implements UserInterface Then i
can we implement polymorphism using interface in c#? any example .
I have an abstract class, AbsClass that implements an interface, IClass . IClass has
I have defined an interface , an abstract class that implements that interface and
Here's the situation: I have an abstract class with a constructor that takes a
I'm using my own generic abstract class (MyAsyncTask) which extends AsyncTasks, and need a
I need to present a modal tab bar controller using interface builder. I'd like
I have refactored some UIView sub-classes into a static library. However, when using Interface
I need help to generify and implement the visitor pattern. We are using tons

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.