cppunit用到单例模式的地方只有一处,那就是在TestFactoryRegistry.cpp文件的TestFactoryRegistryList类中,它提供一个静态类函数如下:
static TestFactoryRegistryList *getInstance()
{
static TestFactoryRegistryList list;
return &list;
}
这里使用了静态变量来实现单例模式,大家可能都习惯使用一个静态指针来实现单例模式,形式如下:
static TestFactoryRegistryList* s_pTestFactoryRegistryList;
if (pTestFactoryRegistryList == null)
return new TestFactoryRegistryList();
else
return s_pTestFactoryRegistryList;
直接将单例对象放到程序的静态存储区,而不是放到堆中是比较有意思的,以前没有见到有这样的做的。
static TestFactoryRegistryList *getInstance()
{
static TestFactoryRegistryList list;
return &list;
}
这里使用了静态变量来实现单例模式,大家可能都习惯使用一个静态指针来实现单例模式,形式如下:
static TestFactoryRegistryList* s_pTestFactoryRegistryList;
if (pTestFactoryRegistryList == null)
return new TestFactoryRegistryList();
else
return s_pTestFactoryRegistryList;
直接将单例对象放到程序的静态存储区,而不是放到堆中是比较有意思的,以前没有见到有这样的做的。