Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

CRTP Factory with less copy-paste

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content