Let's now implement our statically polymorphic class hierarchy. We'll need a base template class:
template <typename ConcreteItem> class GlamorousItem {
public:
void appear_in_full_glory() {
static_cast<ConcreteItem *>(this)->appear_in_full_glory();
}
};
The template parameter for the base class is the derived class. This may seem odd at first, but it allows us to static_cast to the correct type in our interface function, in this case, named appear_in_full_glory. We then call the implementation of this function in a derived class. Derived classes could be implemented like so:
class PinkHeels : public GlamorousItem<PinkHeels> {
public:
void appear_in_full_glory() {
std::cout << "Pink high heels suddenly appeared in all their beauty\n";
}
};
class GoldenWatch : public GlamorousItem<GoldenWatch> {
public:
void appear_in_full_glory() {
std::cout << "Everyone wanted to watch...