C++ Static, Const, and Static Const Member Variable Declarations and Initialization
Category Programming Techniques
Constants defined by const are released from memory once they are out of scope, whereas static constants defined by static do not release their storage space after the function is executed.
Static indicates something that is not changing. Static member functions and variables of a class are related to the class itself, not to a specific object of the class. Even without a specific object, you can call the class's static member functions and variables. Generally, a static function of a class is almost like a global function, except that its scope is limited to the file that contains it.
In C++, static member variables cannot be initialized within the class. They are only declared inside the class, and must be defined outside the class definition, typically initialized in the implementation file of the class, such as: double Account::Rate = 2.25;
The keyword static can only be used in the declaration within the class definition and cannot be marked as static when defining.
In C++, const member variables cannot be initialized at the class definition site; they can only be initialized through the constructor's initialization list and must have a constructor.
Const data members are only constant for the lifetime of an object, but they are variable for the entire class. Since a class can create multiple objects, the value of the const data member can be different for different objects. Therefore, you cannot initialize const data members in the class declaration because the compiler does not know what the value of the const data member will be before the object of the class is created.
The initialization of const data members can only be done in the initialization list of the class constructor. To establish a constant that is constant throughout the entire class, you should use enumeration constants in the class or static const.
class Test{
public:
Test():a(0){}
enum {size1=100,size2=200};
private:
const int a; // Can only be initialized in the constructor's initialization list
static int b; // Defined and initialized in the class implementation file
const static int c; // Same as static const int c.
};
int Test::b=0; // Static member variables cannot be initialized in the constructor's initialization list because they do not belong to a specific object.
const int Test::c=0; // Note: When assigning a value to a static member variable, the static modifier is not needed, but const is required.
Const member functions are mainly intended to prevent member functions from modifying the content of the object. That is, const member functions cannot change the value of member variables, but they can access member variables. When a method is a member function, that function can only be a const member function.
Static member functions are mainly intended to act as global functions within the scope of the class. They cannot access the class's non-static data members. Static member functions of a class do not have a this pointer, which leads to:
- They cannot directly access the class's non-static member variables or call non-static member functions.
- They cannot be declared as virtual.
Regarding the Initialization Issues of Static, Const, Static Const, and Const Static Members
1. Initialization of const members in a class:
When establishing a const within a class, you cannot give it an initial value.
class foo{
public:
foo():i(100){}
private:
const int i=100;//error!!!
};
//Or initialize in this way
foo::foo():i(100){}
2. Initialization of static members in a class:
Static variables in a class belong to the class, not to a specific object. It has only one copy throughout the entire runtime of the program, so you cannot initialize the variable when defining an object, that is, you cannot use a constructor to initialize it. The correct method of initialization is:
Data type Class name::Static data member name=Value;
class foo{
public:
foo();
private:
static int i;
};
int foo::i=20;
This indicates:
- Initialization is performed outside the class, without the static keyword in front to avoid confusion with general static variables or objects.
- The access control symbol such as private, public, etc., is not added during initialization.
3. Initialization of static const and const static members in a class
These two notations have the same effect. To facilitate memory, only one common initialization method is explained here:
class Test{
public:
static const int mask1;
const static int mask2;
};
const Test::mask1=0xffff;
const Test::mask2=0xffff;
//Their initialization is the same, although one is a static constant and the other is a constant static. Both are stored in the global
static const int var44;
public:
Test(void);
~Test(void);
};
//--------------------Test.cpp-----------------------------------
#include ".\test.h"
int Test::var3 = 3333333; // Correct initialization method for static members
// int Test::var1 = 11111;; Error, only static members can be initialized
// int Test::var2 = 22222; Error
// int Test::var44 = 44444; // Incorrect method, causes redefinition error
Test::Test(void): var1(11111), var2(22222) Correct initialization method // var3(33333) cannot be initialized here
{
var1 = 11111; // Correct, ordinary variables can also be initialized here
// var2 = 222222; Error, because constants cannot be assigned, they can only be initialized in the "constructor initializer (constructor initialization list)"
var3 = 44444; // This assignment is correct, but since all objects have one static member, it will affect others, so this cannot be called initialization anymore
}
Test::~Test(void){}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Some member variables have special data types, and their initialization methods differ from those of ordinary data type member variables. These special types of member variables include:
- a. Constant member variables
- b. Reference member variables
- c. Static member variables
- d. Integral static constant member variables
- e. Non-integral static constant member variables
For the initialization of constant member variables and reference member variables, it must be done through the constructor initialization list. It is not feasible to assign values to constant member variables and reference member variables within the body of the constructor.
The initialization of static member variables is also quite special.
Refer to the following code and its comments:
// Initialization of Special Data Member
include <iostream>
using namespace std;
class BClass
{
public:
BClass() : i(1), ci(2), ri(i) // For constant member variables and reference member variables, initialization must be done through
{ // the parameter list. Assigning values within the constructor body is not feasible.
}
void print_values()
{
cout << "i =\t" << i << endl;
cout << "ci =\t" << ci << endl;
cout << "ri =\t" << ri << endl;
cout << "si =\t" << si << endl;
cout << "csi =\t" << csi << endl;
cout << "csi2 =\t" << csi2 << endl;
cout << "csd =\t" << csd << endl;
}
private: int i; // Ordinary member variable const int ci; // Constant member variable int &ri; // Reference member variable static int si; // Static member variable // static int si2 = 100; // error: Only static constant member variables can be initialized like this static const int csi; // Static constant member variable static const int csi2 = 100; // Static constant member variable initialization (Integral type) (1) static const double csd; // Static constant member variable (non-Integral type) // static const double csd2 = 99.9; // error: Only static constant integral data members can be initialized in the class }; // Static member variable initialization (Integral type) int BClass::si = 0; // Static constant member variable initialization (Integral type) const int BClass::csi = 1; // Static constant member variable initialization (non-Integral type) const double BClass::csd = 99.9;
// When initializing csi2 in (1), according to Stanley B. Lippman, the following line is necessary. // However, in VC2003, having the following line will cause an error, while in VC2005, this line is optional, which depends on the compiler. const int BClass::csi2;
int main(void) { BClass b_class; b_class.print_values(); return 0; } ```
c++ Static Member Summary, c++, static
After reflecting on my pain, I decided to summarize the knowledge points of static class members, so that I will not be passive in such issues in future interviews.
Static class members include two parts: static data members and static function members.
Static Data Members
By adding the keyword static before the declaration of data members in the class body, the data member becomes a static data member of the class. Like other data members, static data members also follow the public/protected/private access rules. At the same time