何时创建和销毁特定 C++ 类的静态成员?假设我定义了一个WinVersion
类:
WinVersion.h
class WinVersion {
public:
// static methods
static WinVersion& Get();
static bool Is_NT();
// singleton
static WinVersion m_version;
// constructor
WinVersion();
private:
unsigned short m_PlatformId;
unsigned short m_MajorVersion;
unsigned short m_MinorVersion;
unsigned short m_BuildNumber;
};
WinVersion.cpp:
// static members
WinVersion WinVersion::m_version = WinVersion(); // unsure if it's good enough
// static functions
WinVersion& WinVersion::Get() {
return m_version;
}
bool WinVersion::Is_NT() {
return (m_version.m_PlatformId == VER_PLATFORM_WIN32_NT);
}
// constructor
WinVersion::WinVersion()
{
OSVERSIONINFO osinfo;
osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
...
}
什么时候m_version
创建和销毁静态成员?万一出现异常怎么办?