A function is a major component of a C ++ program. A function can call other functions. In a well-designed program, each function has a specific purpose, that is, it is automatically called by. NET before the first instance is created or any static member is referenced.
Static constructor is a new feature of C ++, which is rarely used. However, we need to use static variables when initializing them. This constructor belongs to a class rather than an instance. That is to say, this constructor will only be executed once.
The following is a reference clip:
- class SimpleClass
- {
- // Static constructor
- static SimpleClass()
- {
- //
- }
- }
Pay attention to the following points when using the C ++ static constructor:
1. The static constructor neither has an access modifier nor a parameter. Because it is called by. NET, modifiers such as public and private are meaningless.
2. When the first class instance or any static member is referenced ,. NET will automatically call the static constructor to initialize the class. That is to say, we cannot directly call the static constructor, so we cannot control when to execute the static constructor.
3. A class can have only one static constructor.
4. A non-parameter constructor can coexist with a static constructor. Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.
5. Run only once at most.
6. Static constructors cannot be inherited.
7. If the C ++ static constructor is not written and the class contains a static member with an initial value, the compiler automatically generates the default C ++ static constructor.
Now let's look at the problems of colon initialization and function initialization in constructor. The function of class constructor is to call it to construct the data members of this class object when creating a class object. First, we need to allocate memory space for this data member. Second, we need to initialize the function data member and construct the data member in the Order stated by the data member in the class.
The difference between colon initialization and function body Initialization is:
The colon (:) Initialization is performed when a data member is allocated memory space, that is, a data member is assigned a value expression after the colon (this expression must be a value expression in parentheses ). After the memory space is allocated, the data member is assigned a value before entering the function body.
That is to say, the function body has not yet executed before initializing this data member. Therefore, this mechanism is added in C ++, which is required for object-oriented programming. I don't know. If you do not understand, please refer to the relevant information. In the above program, compilation is not passed.
The compilation system will tell you that the teacher Class Object lacks the default constructor, because the default constructor is not defined in the teach class. How can a constructor with parameters be constructed? Assign values using the colon as mentioned above. The Student class has two data members.
One is a constant data member, the other is a reference data member, and the two data members are initialized in the constructor, but this cannot be compiled because the constant must be assigned a value during initialization, its value can no longer be changed, and a value must be assigned for reference initialization like a constant. After a reference is defined, it is maintained together with the referenced target and cannot be assigned again.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design