boost singleton模式VC6编译的问题
Singleton的源码如下:
[C/C++ code]
template <typename T>
struct singleton
{
private:
struct object_creator
{
object_creator() { singleton<T>::instance(); }
inline void do_nothing() const {};
};
static object_creator create_object;
public:
typedef T object_type;
static object_type & instance()
{
static object_type obj;
create_object.do_nothing();
return obj;
}
};
template <typename T> typename singleton<T>::object_creator singleton<T>::create_object;
需要使用Singleton模式的类
[C/C++ code]
class SingleObject
{
public:
SingleObject()
{
}
virtual ~SingleObject()
{
}
private:
int m_a;
};
我在main函数当中去如下调用
[C/C++ code]
SingleObject& testSingleton = singleton<SingleObject>::instance ();
可是编译的时候会出现错误:
error C2248: 'object_creator' : cannot access private struct declared in class 'singleton<class SingleObject>'
see declaration of 'object_creator'。
这个错误的原因是什么?我在VS2003下编译就不会存在这样的问题。
[解决办法]
那就用VS2003吧