initialization of three types of data
- Initialization of 1.static int a
- Initialization of Const int a
- Initialization of the static const int a
three ways of initializing
- Initializing outside the class
- Class in the constructor by initializing the list.
- initializing at the same time as the declaration
A. static data member Static member: A member in a static class that joins the static modifier, which is a static member. You can access this static member directly using the class name. static member name.
because static members exist in memory, non-static members need to be instantiated to allocate memory, so static member functions cannot access non-static members: Because static members exist in memory, non-static member functions can directly access static members of the class.a static data member has only one copy in the program and is shared by all objects of that type. static data members are stored in the
global data area .
static data members are defined when they are allocated space, so they cannot be defined in a class declaration . It does not belong to a particular class object, and its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class;the static data member is initialized in the following format:
Data type >< class name >::< static data member name >=< value
The static data members of a class are accessed in two ways:
Class object name >.< static data member name or class type name >::< static data member name
two. const data member initialization modeInitialization by constructor list, cannot be initialized in constructor
Summary:static int A to initialize the const int A in the constructor initializer list to initialize the static const int A can be initialized at the time of declaration, or it can be initialized outside the class. However, you cannot initialize the list by constructor initialization.
Static data members and const data members are defined and initialized