January 2019
Intermediate to advanced
512 pages
14h 5m
English
We will now focus on using CRTP to avoid writing the clone() function in every derived class. This is not just done to reduce typing—the more code is written—especially the very similar code that gets copied and modified—the more likely you are to make an error. We have already seen how to use CRTP to generate a version of clone() for every derived class automatically. We just don't want to give up the common (non-template) base class to do so. We don't really have to do so if we delegate the cloning to the special base class that handles only that:
class Base { public: virtual Base* clone() const = 0;};template <typename Derived> class Cloner : public Base { public: Base* clone() const { return new Derived(*static_cast<const ...