Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ forcing operator == overload from interface abstract class

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 ?

like image 381
Oded Avatar asked Jan 01 '13 15:01

Oded


People also ask

Does overloading == also overload !=?

NO. There is no such requirement that you Must overload != If you need to overload == . However,it is a good practice that you Should overload operators related to each other.

How do I overload operator+?

Overloading the plus operator (+) is as simple as declaring a function named operator+, giving it two parameters of the type of the operands we want to add, picking an appropriate return type, and then writing the function.

Can we overload == operator in C++?

Overloading Binary Operators Suppose that we wish to overload the binary operator == to compare two Point objects. We could do it as a member function or non-member function. To overload as a member function, the declaration is as follows: class Point { public: bool operator==(const Point & rhs) const; // p1.

Can we change the behavior of overloaded operator?

Operators play an important role in computer programming. By using it, you can change the behavior of an operator based on the types of its arguments.


1 Answers

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...

like image 124
benjarobin Avatar answered Sep 28 '22 05:09

benjarobin