C ++ class const, static, static const member variables
# Include
Using namespace std;
Class Test
{
Private:
Const int a; // The const member variable can only be initialized in the constructor member initialization list, not in the function body and elsewhere
Static int B; // static member variables must be initialized globally. Format: type name Class Name: variable name = Value
Static const int c; // static const member variables must be initialized globally. Format: type name Class Name: variable name = Value
Public:
Test (): a (1) {}// correct
// Test () {a = 1;} // Error
Int get_a ()
{
Return;
}
Int get_ B ()
{
Return B;
}
Int get_c ()
{
Return c;
}
};
Int Test: B = 2;
Const int Test: c = 3;
Int main ()
{
// Int Test: B = 2; Error
// Const int Test: c = 3; Error
Test temp;
Cout <temp. get_a () <endl <temp. get_ B () <endl <temp. get_c () <endl;
Cin. get ();
Return 0;
}