Skip to content

Instantly share code, notes, and snippets.

@ndtimofeev
Last active November 9, 2017 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndtimofeev/2bcef9e4118a5417f21d0308dd0160e2 to your computer and use it in GitHub Desktop.
Save ndtimofeev/2bcef9e4118a5417f21d0308dd0160e2 to your computer and use it in GitHub Desktop.
#include <iostream>
template <typename C>
struct Base
{
static const int id;
static constexpr const char* const name = 0;
static void out(){ std::cout << "name: " << C::name << std::endl; }
};
struct DerivedA : public Base<DerivedA>
{
static const int id = 1;
static constexpr const char* const name = "DerivedA";
};
struct DerivedB : public Base<DerivedB>
{
static const int id = 2;
static constexpr const char* const name = "DerivedB";
};
int main( int argc, char** argv )
{
DerivedA da;
DerivedB db;
std::cout << da.name << " " << db.name << std::endl;
std::cout << da.id << " " << db.id << std::endl;
std::cout << sizeof( da ) << " " << sizeof( db )<< std::endl;
da.out();
db.out();
std::cout << "That's all!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment