How do you “not repeat yourself” when giving a class an accessible “name” in C++?
Consider the following: class Base { public: virtual std::string getName() = 0; ... }; class Derived1 : public Base { public: static std::string getClassName() { return("Derived1"); } std::string getName() { return("Derived1"); } ... }; class Derived2 : public Base { public: static std::string getClassName() { return("Derived2"); } std::string getName() { return("Derived2"); } ... }; The idea is that if you have the derived class passed as, say, a template parameter, then you can get its class name via getClassName , while if you have it passed as a pointer to base class, you can get the name