Document directory
After so many years of development, C ++ has become a culture and art, which is not inherent in C ++, it is the summary and artistic result of C ++'s applications in various aspects. C ++ looks complicated, but in depth, you will find that C ++ is so beautiful and philosophical. To make C ++ more artistic, the C ++ language masters have paid a lot for it. They are all pursuing simplicity and programming.
Almost all programming languages have initialization methods in one way or another. Newer languages such as C # integrate many initialization technologies into the compiler, many programming languages require programmers to use specific methods. C ++ Initialization Technology relies heavily on programmers. This article describes several common and practical initialization techniques.
1. Initialization during compilation
The so-called compile-time initialization refers to the determination of the symbolic value during the compile-time. It generally includes the initialization of common constants and the initialization technology that has used the template technology to construct constants.
1.1. Constant
For example, const int scale = 1;
Enum colorspace {rgbcolorspace, cmykcolorspace };
Class Example {
Static const int COUNT = 1;
}
1.2. Use templates
This is one of the most basic techniques used in template metaprogramming. This technique uses the compiler to recursively compile the template to initialize a constant. For example:
Template Factorial;
Template <> factorial <1 >{enum {result = 1 };};
Template <> factorial {Enum {result = N * factorial : Result };};
2. Static Initialization
Static initialization refers to the initialization of Global (static) variables or static member programming behavior when the program starts.
2.1. initialize global variables
For example, int g_globalcount = 1;
Static int g_s_globalcount = 1;
2.2. static member variable Initialization
For example, class example {
Static example nullexample;
}
Example example: nullexample;
3. Dynamic Initialization
This initialization technique utilizes the construction of classes and the basic features of destructor.
If we need to register a class creation function to the class factory when the program starts, we can do it as follows:
//////////////////////////////////////// //////////////////////////////////////// /// // Defines the macros required for initialization.
Typedef geometry * (* createfuncptr )();
Struct auto_register {
Auto_register (createfuncptr func ){//Registration}
~ Auto_register (){//Cancel registration}
};
# Define auto_register (class_name )/
Static const auto_register _ register # class_name # (class_name: creategeometry );
//////////////////////////////////////// //////////////////////////////////////// //// // Defines the implementation based on the above Initialization Technology
Class geometry {
Public: static geometry * creategeometry (){...}
}
Class diamondgeometry {
Public: static diamondgeometry * creategeometry (){...}
}
Auto_register (diamondgeometry)
Class rectanglegeometry {
Public: static rectanglegeometry * creategeometry (){...}
}
Auto_register (rectanglegeometry)