It will have static lifetime and be destroyed after
main returns:
static std::auto_ptr<T> thePointer;
Another option is to register your own atexit function:
// static
void YourClass::freePointer(void)
{
delete getPointer();
}
// static
T* YourClass::getPointer(void)
{
if (!thePointer)
{
thePointer = new T;
atexit(freePointer);
}
return thePointer;
}
Which will have the same effect.
http://stackoverflow.com/questions/2429408/c-freeing-static-variables
本文探讨了C++中静态变量的生命周期及释放方式。一种方法是在主函数返回后销毁静态std::auto_ptr。另一种方法是注册一个atexit函数,在程序退出时删除静态指针。这两种方法都能确保静态变量被正确清理。
1428

被折叠的 条评论
为什么被折叠?



