A const-defined constant is freed after its scope has been exceeded, and static constants defined by Statics do not release their storage space after the function executes.
Static is expressed statically. Static member functions and static member variables of a class are related to the class, not to the specific object of the class. Static member functions and member variables of a class can be called even if there are no specific objects. A static function of a general class is almost a global function, except that it is scoped to the file containing it.
In C + +, static statically member variables cannot be initialized inside a class. Inside a class is just a declaration, the definition must be outside the body of the class definition, usually initialized in the class's implementation file, such as: The double account::rate=2.25;static keyword can only be used in declarations within the class definition body, and cannot be marked as static when defined
In C + +, const member variables cannot be initialized at the class definition, only through constructor initialization lists, and must have constructors.
Const data members are constants only for the lifetime of an object, but are mutable for the entire class. Because a class can create multiple objects, different objects can have different values for their const data members. The const data member cannot be initialized in the declaration of the class, because the compiler does not know what the value of the Const data member is when the object of the class is not created.
The initialization of a const data member can only be done in the initialization list of the class's constructor. To build constants that are constant throughout a class, you should use enumerated constants in the class, or static cosnt.
[CPP]View Plaincopy
- Class Test
- {
- Public
- Test (): A (0) {}
- enum {size1=100,size2=200};
- Private
- const int A; Can only be initialized in the constructor initialization list
- static int b; Defined and initialized in the implementation file of the class
- const static int C; Same as static const int C;
- };
- int test::b=0; The //static member variable cannot be initialized in the constructor initialization list because it does not belong to an object.
- cosnt int test::c=0; Note: When assigning a value to a static member variable, you do not need to add the static modifier. But to add cosnt .
The main purpose of the COSNT member function is to prevent member functions from modifying the contents of an object. That is, the const member function cannot modify the value of a member variable, but it can access the member variable. When a method member function, the function can only be a const member function.
The static member function is primarily intended as a global function for the scope of a class. You cannot access non-static data members of a class. The static member function of the class does not have the this pointer, which causes: 1, cannot directly access non-static member variables of the class, call non-static member function 2, cannot be declared as virtual
Initialization problems with static, const, static cosnt, const static members:
1. Const member initialization in class:
When you create a const in a class, you cannot give him an initial value.
[CPP]View Plaincopy
- Class Foo
- {
- Public
- Foo (): I (100) {}
- Private
- const int i=100; Error!!!
- };
- Or in such a way as to initialize
- Foo::foo (): I (100)
- {}
2. Initialization of static members in class:
A static variable in a class belongs to a class, does not belong to an object, it has only one copy during the entire program's run, so the variable cannot be initialized when the object is defined, or it cannot be initialized with a constructor, the correct initialization method is:
Data type class Name:: static data member name = value;
[C-sharp]View Plaincopy
- Class Foo
- {
- Public
- Foo ();
- Private
- static int i;
- };
- int foo::i=20;
- This shows that:
- 1. Initialization is performed outside the class body, and the front is not static, so as to avoid confusion with general static variables or objects
- 2. Initialization without the access control of the member private, public, etc.
- 3. Initialization uses a scope operator to indicate the class to which it belongs, so a static data member is a member of a class rather than a member of an object.
3. Static cosnt and const static member initialization in class
In the same way that these two formulations work, in order to facilitate memory, this value describes a common initialization method:
[CPP]View Plaincopy
- Class Test
- {
- Public
- static const int mask1;
- const static int mask2;
- };
- Const TEST::MASK1=0XFFFF;
- Const TEST::MASK2=0XFFFF;
- There is no difference in their initialization, although one is a static constant and one is constant static. The static is stored in the global variable area, in fact the final result is the same. may be handled differently within different compilers, but the final result is the same.
This is a complete example:
[CPP]View Plaincopy
- #ifdef A_h_
- #define A_h_
- #include <iostream>
- Using namespace std;
- Class A
- {
- Public
- A (int a);
- static void print (); Static member functions
- Private
- static int aa; Declaration of a static data member
- static const int count; Constant static data member (can be initialized in constructors)
- const int bb; Constant data members
- };
- int a::aa=0; //Definition of static member + initialization
- const int a::count=25; Static constant member Definition + initialization
- A::A (int a): BB (a)//initialization of constant members
- {
- Aa+=1;
- }
- void A::p rint ()
- {
- cout<<"count=" <<count<<endl;
- cout<<"Aa=" <<aa<<endl;
- }
- #endif
- void Main ()
- {
- A (10);
- A::p rint (); //Access static member functions by class
- A.print (); //accessing static member functions through objects
- }
The difference between const and static in C + + (GO)