65.9K
CodeProject is changing. Read more.
Home

Quickly check whether C++ template instances have the same parameters

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (2 votes)

Feb 7, 2012

CPOL
viewsIcon

8888

If you're using templates already, and are willing to adapt your class definitions, then this should work:template class BaseClass {public: virtual bool equals(T* other) = 0;};template class ChildClass : public BaseClass > {public: typedef...

If you're using templates already, and are willing to adapt your class definitions, then this should work:
template <class T>
class BaseClass {
public:
   virtual bool equals(T* other) = 0;
};

template <class T>
class ChildClass : public BaseClass<ChildClass<T> > {
public:
   typedef ChildClass<T> thistype;
   virtual bool equals(thistype* other) {
      bool result(false);
      // no cast needed!
      // now do your work here ...
      return result;
   }
};
Note how the definition of the child class passes its own type down to the base class as a parameter, and thereby defines the argument type of the function equals().