June 2025
Intermediate to advanced
1093 pages
33h 24m
English
Assume you have defined a Tree class in the tree.hpp header and used the header multiple times in your program. Then you must ensure that the initialization of the static data fields is done in exactly one *.cpp file and not in the *.hpp file as they would otherwise be defined multiple times.
Therefore, starting from C++17, in addition to static, write the inline keyword and set the field directly—that is, the declaration and definition together. Then the compiler ensures unique initialization:
class Tree { static inline size_t countConstructed_ = 0; static inline size_t countDestructed_ = 0; // …
Read now
Unlock full access