I personally think it is more convenient to use
# Define SINGLETON_CLASS_NO_DEF_CONSTRUCT_BODY (class_name )\
Private :\
Class_name ();\
Class_name (const class_name &);\
Class_name & operator = (const class_name &);\
Public :\
Static class_name & Instance ()\
{\
Static class_name one ;\
Return one ;\
}
# Define SINGLETON_CLASS (class_name )\
Private :\
Class_name (){}\
Class_name (const class_name &);\
Class_name & operator = (const class_name &);\
Public :\
Static class_name & Instance ()\
{\
Static class_name one ;\
Return one ;\
}
The difference between two macros is only that the default no-argument constructor is implemented. Example:
// HostBasicInformation. h
Class HostBasicInformation
{
SINGLETON_CLASS_NO_DEF_CONSTRUCT_BODY (HostBasicInformation)
Public:
Virtual ~ HostBasicInformation ();
Std: string GetInstallCode ();
};
// HostBasicInformation. cpp
// If SINGLETON_CLASS is used, no parameter-free constructor needs to be defined.
HostBasicInformation: HostBasicInformation (){
}
HostBasicInformation ::~ HostBasicInformation (){
}
Std: string HostBasicInformation: GetInstallCode ()
{
Return "";
}
The main advantage of this macro method is that it can strictly limit the use of this type, that is, it can only be used as a singleton.
Std: string str_installCode = HostBasicInformation: Instance (). GetInstallCode ();