I’m happy to announce that my new book on Data Member Initialization is published!

Have a look at the background story and how to get it.

About the book  

As for my other books, I used the Leanpub platform for the PDF/Epub generation. Here’s the book’s website:


Data Member Initialization in Modern C++ @Leanpub

Why I wrote the book?  

Initialization in C++ is a hot topic. With each C++ Standard, we’ll get some updates to the syntax, rules, and new capabilities. With this book, I wanted to clarify the confusion and make it simpler to reason about Modern C++ code.

Have I succeeded?

It’s C++, and you’ll see many opinions, special cases, and even contradicting examples. Yet, I’m happy with my content, and it should certainly improve your knowledge and expand your views.

Learning objectives  

The goal is to equip you with the following knowledge:

  • The basic rules of data members and constructors.
  • Essential information about copy constructors, move constructors, and destructors.
  • How to efficiently initialize non-static data members using C++11 features like non-static data member initialization, inheriting, and delegating constructors.
  • How to streamline working with static data members with inline variables from C++17.
  • How to work with container-like members, non-copyable data members like const data members, or move-able only data members.
  • Designated initializers from C++20.

The structure of the book  

The book contains 15 chapters in the following structure:

  • Chapters 1 to 5 create a foundation for the rest of the book. They cover constructors, destructors, and the basics of data members.
  • Chapter 6 shows a use case for passing strings into constructors.
  • Chapter 7 is a short quiz on constructors. You can check your knowledge from the first “part” of the book.
  • Chapter 8 describe Non-static Data Member Initialization (NSDMI), a powerful feature from C++11 that improves how we work with data members.
  • Chapter 9 serves as a practical summary for NSDMI, where you can solve a few exercises.
  • Chapter 10 discusses how to initialize container-like data members.
  • Chapter 11 contains information about non-regular data members and how to handle them in a class. You’ll learn about const data members, unique_ptr as a data member, and references.
  • Chapter 12 shows how inline variables from C++17 work.
  • Chapter 13 moves to C++20 and describes Designated Initializers, a handy feature based on similar thing from the C language.
  • Chapter 14 it’s the summary chapter with a demo that gathers most of the features described in the book.
  • Chapter 15 is the final quiz with seven questions ranging from constructors to designated initializers.

Background for the book  

Long story short: a few years ago, I wrote an article about NSDMI and got curious about how things work for data members. Last year I created a mini-course on the Educative platform. Starting this year, I wanted to bring the PDF version… and it looks like I discovered and described even more stuff :)

Here’s the initial article from 2015: Non Static Data Members Initialization - C++ Stories. And the course at Educative: Initializing Data Members: From C++11 till C++20.

Initially, after converting the lessons into a pdf, I got something around 60 pages. But then I realized it would be better to describe things with more examples and use cases. After digging, investigating, talking to various people… I ended up with 100 pages and now with 169 pages :) While the page count is not the only factor for the book, I believe it corresponds to the quality and practical knowledge I put inside.

As a new experiment, I put quizzes and exercises so that you can practice and check your knowledge. There’s no need to set up any coding environment for those tasks, as you can just use Compiler Explorer links and run all the code in the browser. This also works for 50+ examples you can find across the book - open the link and see how the code works.

Updates and plans  

The book is 95% ready, and a few sections are still missing. I hope to complete them in July and August. If everything goes fine, then I’ll try to prepare the paper-back version in Fall.

Currently, the price is set to the minimum price available at Leanpub, but when the book is ready, it will go up to 9.99$.

Acknowledgements  

This book wouldn’t be possible without valuable input from many C++ experts and friends.

I especially would like to thank to the following people:

They spent a lot of time finding even little things that could be improved and extended.

Last but not least, I got a lot of feedback and comments from the blog readers, Patreon Discord Server (See @C++Stories @Patreon), and discussions at C++ Polska. Thank you all!

With all of the help from those kind people, the book quality got better and better!

And please join the reviewer list by providing me with your suggestions and improvements. I’m happy to update the book.

How to Get the Book??  

Here’s the link to the book @Leanpub:


Data Member Initialization in Modern C++ @Leanpub

Leanpub offers a 60-day refund period!

And several options to get it cheaper:

You can also download a free sample that contains almost 60 pages (available from the Leanpub page)!