Click here to Skip to main content
12,890,879 members (56,553 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 7 Feb 2012

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

, 7 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 {
   virtual bool equals(T* other) = 0;
template <class T>
class ChildClass : public BaseClass<ChildClass<T> > {
   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().


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


About the Author

Software Developer (Senior)
Switzerland Switzerland
Graduated at TU Darmstadt in Math & CS, with a heavy focus on CAD/CAM

Programming and designing applications in C++ in the areas AI, real-time programming, client-server applications and CAD/CAM since 1985.

Personal interests: AI, computer graphics, games, reading

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Yoldas Askan15-Sep-14 7:01
memberYoldas Askan15-Sep-14 7:01 
GeneralUnfortunately my code didn't allow for the base class to hav... Pin
Sebastian Krysmanski7-Feb-12 21:15
memberSebastian Krysmanski7-Feb-12 21:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 7 Feb 2012
Article Copyright 2012 by Stefan_Lang
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid