January 2019
Intermediate to advanced
512 pages
14h 5m
English
As we have just seen, CRTP can be used to allow the derived class to customize the behavior of the base class:
template <typename D> class B { public: ... void f(int i) { static_cast<D*>(this)->f(i); } protected: int i_;};class D : public B<D> { public: void f(int i) { i_ += i; }};
If the base class B::f() method is called, it dispatches the call to the derived class method for the real derived class, just like a virtual function does. Of course, in order to fully take advantage of this polymorphism, we have to be able to call the methods of the base class through the base class pointer. Without this ability, we are simply calling methods of the derived class whose type we already know:
D* d = ...; // Get an object ...