Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

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

4.56/5 (2 votes)
7 Feb 2012CPOL 8.7K  
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:
C++
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().

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)