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++

Introducing CRTP

Now, we will introduce CRTP, which turns inheritance on its head:

template <typename D> class B {    ...};class D : public B<D> {    ...};

The first change that jumps out is that the base class is now a class template. The derived class still inherits from the base class, but now from the specific instantiation of the base class template—on its own! Class B is instantiated on class D, and class D inherits from that instantiation of class B, which is instantiated on class D, which inherits from class B, which... that's recursion in action. Get used to it because you will see it often in this chapter.

What is the motivation for this mind-twisting pattern? Consider that now the base class has compile-time information about the derived ...

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