DevReads.sandordargo.com

Hands-On Design Patterns with C++ by Fedor Pikus

In Hands-On Design Patterns with C++, the author Fedor Pikus shares the most common design patterns used in modern C++. In fact, not only design patterns but also some best practices and idioms. He doesn’t end the stories simply where the Gang of Four Design Patterns book ends, but he looks deeply into the specialities of C++ implementations.

But what does the book cover?

It starts with discussing inheritance and polymorphism in general. Then it goes to detailing templates and how to do generic programming in C++. While it seems a bit strange to discuss templates right after inheritance, if you think about it a bit more, it’s not odd at all. Polymorphism can be achieved dynamically with the good old virtual functions or statically, using templates heavily. The author reaches the Curiously Recurring Template Pattern a bit before the half of the book and that pattern accompanies us in many subsequent chapters as a building block for more complex solutions.

I particularly enjoyed the chapter that presented Type Erasure which has growing popularity in the C++ community. The author taught me something in this subject that I haven’t really considered before. Notably that shared_ptr is not slower than unique_ptr only because shared pointers do reference counting. No. Shared pointers also erase the type of the optional deleter and that has a cost!

I won’t enumerate all the topics, the author discusses across the 18 chapters of this book, you can have a look at this sample on Amazon, but I have to mention that Chapter 10 greatly improved my understanding of local buffer and small strings optimization, what they are and how they are implemented.

In chapter 14, the template method pattern (which has nothing to do with C++ templates) is on the table. You might simply know the Non-Virtual Idiom. Combine them and you get a very C++ centric idiom.

I already used it our code base since to refactor a big piece of code and remove some hundreds of copied lines. The basic idea behind is that the public API of your classes only provide a non-virtual interface, keeping it fairly simple.

No need to think about what implementation is called in the end. On the other hand, the non-virtual base class function has branching points where it calls usually pure virtual private functions and the differences are concantrated at those places. I’ll write about this idiom in more lengths in the coming months.

I also enjoyed the chapter on policy-based design, which is just a fancy name of the good old strategy pattern from the GOF. I liked the way the author explained it and how he applied the pattern at compile time using generic programming. In fact, I think this whole book helped me a lot in understanding better programming with templates.

If you are looking for a book that will give you practical knowledge on how to design a modern C++ application, this is the one you should read! A highly recommended read!

Connect deeper

If you liked this article, please

Sandor Dargo

C++ Developer, writer, speaker, traveller, happy father of two

Your subscription could not be saved. Please try again.
Thanks for subscribing. You'll receive our next monthly newsletter.

Subscribe

Get a monthly review on the best books for developers!