c++ 的单例类的析构函数应该是public还是非public,请看代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
class CTest
{
public:
static CTest& GetInstance()
{
static CTest obj;
return obj;
}
void Hello()
{
printf("hello -----------\n");
}
~CTest()
{
printf("~CTest() called------\n");
}
private:
CTest()
{
printf("CTest() called------\n");
}
};
int main()
{
printf("main is called\n");
CTest::GetInstance().Hello();
delete &(CTest::GetInstance());
printf("main is exit\n");
return 0;
}
编译,运行,肯定报错,因为单例对象是static,系统不允许delete的
安全起见,还是声明private,或者protected吧