Storage duration and initialization
C++ objects with automatic storage duration are initialized upon declaration and destroyed when exiting the variable scope. Objects can also have a static storage duration. Data members of objects can also have static storage specifiers, and there are rules for the initialization of such members. We will first go through non-static member initialization.
Non-static member initialization
There are different ways to initialize non-static class members. The first thing that comes to mind when we discuss initialization and C++ is constructors. While constructors are powerful C++ features that allow us to have great control over the initialization, let us start with default member initializers.
Default member initializers
As of C++11, it is possible to set a default value for a member directly in a class definition, as follows:
class my_class{
int a = 4;
int *ptr = nullptr;
}
This simple code snippet would fail to compile...