头文件
#ifndef C_TOOLS
#define C_TOOLS
class tool
{
tool();
~tool();
public:
static tool * getTool(); // 获取tool的实例对象
int getRandMinMax(int min, int max);
};
#endif
cpp文件
static tool * g_tool = nullptr;
tool::tool()
{
time_t t;
time(&t);
srand(t);
}
tool::~tool()
{
}
tool * tool::getTool()
{
if (g_tool == nullptr)
{
g_tool = new tool();
}
return g_tool;
}
int tool::getRandMinMax(int min, int max)
{
int ret = rand() % (max - min) + min;
return ret;
}
static tool * getTool();这句代码必须写成静态函数么?不用静态可以么 静态和析构为什么写成私有的?