List of articles in this column
First, what is object-oriented
Second, C language can also achieve object-oriented
Third, the non-elegant features in C + +
Iv. solve the package and avoid the interface
V. Rational use of templates to avoid code redundancy
VI, C + + can also reflect
Vii. single-Case pattern solving the construction order puzzle of static member objects and global objects
Viii. more advanced preprocessor PHP
Vii. single-Case pattern solving the construction order puzzle of static member objects and global objects
The last book says that our program has a hidden loophole, if the. o file where this class is located is the first one to be linked in all. o files, then there is no problem with classregister.
That's so abstract, let's draw a chart.
ClassRegister.o--------------------Meta.o--------------------Main.o
Such a structure, that is, the link order to specify
gcc -o main ClassRegister.o Meta.o Main.o
There will be no problems, but if you change the order you may have problems.
Think about the reason, the map object in Classregister, is a global object, and when we register the class, we also use the global object's constructor, two who first execute it? This is not known, C + + does not explain the two who first who, and the general linker, is the previous link code, and the execution order of the constructors, also often related to the order of links.
But this implementation is very bad, our system actually depends on the linker order to properly compile the execution, too unreliable, in case users do not notice this, directly compile the link, there will be an unknown error.
So how to avoid this situation?
A singleton mode for C + +
C + + has an excellent design pattern is suitable for this situation, that is, with Singleton, Singleton mode is also easy to understand, the core is deferred construction, if not used, it will not be constructed, when used, the object will be constructed, and only once, the most common way is:
class CSingleton { private: CSingleton() //构造函数是私有的 { } static CSingleton *m_pInstance; public: staticGetInstance() { if(m_pInstance == NULL) //判断是否第一次调用 new CSingleton(); return m_pInstance; } };
Of course, we do not consider multi-threading here, because multi-threaded singleton mode is generally locked to ensure that no multiple constructs will cause a conflict.
So after a brief modification, you can design a class registrar with a singleton pattern:
classclassregister{Private: Classregister () {printf("register\n"); }//constructor is private STD:: Map<const std::string, imetaclass*>Class_map; Public:StaticClassregister * getinstance () {StaticClassregister instance;//local static variable return&instance; }Static voidADD (Const STD::stringS, imetaclass* k) {getinstance ()->class_map[s] = k; }StaticImetaclass* Get (Const STD::strings) {STD:: Map<const std::string, imetaclass*>& M = getinstance ()->class_map;if(M.find (s)! = M.end ())returnM[s];Else returnNULL; }};
This type of registrar is simple and practical, with a slightly different pattern of design and pointers, using the concept of local static variables.
Local static variables can change the life cycle of an object so that it fits our requirements well.
Object-oriented topics for C and C + + (7)--Singleton pattern solving the construction order puzzle of static member objects and global objects