初始化"T&"类型的静态成员的正确方法是什么在模板化的类中? [英] What is correct way to initialize a static member of type 'T &' in a templated class?
问题描述
我正在玩一个渴望初始化的通用单例类.这个想法是您像这样从类中公开继承:
I'm playing around with an eager-initializing generic singleton class. The idea is that you inherit publicly from the class like so:
class foo : public singleton<foo> { };
我在此过程中学到了很多东西,但是由于它破坏了我的Visual Studio 2008链接程序,我现在陷入了困境.问题出在静态实例成员和/或其初始化上.
I've learned a lot in the process but I'm stuck right now because it's breaking my Visual Studio 2008 linker. The problem is with the static instance member and/or its initialization.
template<class T>
class singleton {
singleton();
singleton(singleton const &);
singleton & operator = (singleton const &);
public:
static T & instance;
};
template<class T> T & T::instance;
任何见识将不胜感激!
使用此类声明...
template<class T>
class singleton {
singleton();
singleton(singleton const &);
singleton & operator = (singleton const &);
public:
static T instance;
};
template <class T> T singleton<T>::instance;
当我尝试这样做时...
When I try to do this...
class foo : public singleton<foo> { };
我收到此错误...
错误C2248:"singleton :: singleton" :无法访问私人会员 在类单身"中声明
error C2248: 'singleton::singleton' : cannot access private member declared in class 'singleton'
...
此诊断发生在编译器生成的函数'foo :: foo(void)'
This diagnostic occurred in the compiler generated function 'foo::foo(void)'
我的解释是,单例想要构造一个foo对象,该对象通过继承取决于构造函数为私有的单例的构造.我认为单例可以访问其自己的构造函数,但我想没有.有什么想法吗?
My interpretation is that singleton wants to construct a foo object which, by inheritance, depends on the construction of a singleton whose constructor is private. I figured singleton would have access to its own constructor but I guess not. Any ideas?
我已经意识到,从singleton<T>
继承的方法存在一个问题,即要求更改类以用作单例.对于我急切初始化的单例类模板,我结束了以下代码.
I've realized that the approach of inheriting from singleton<T>
has the problem of requiring change to the class to be used as a singleton. I've ended up with the following code for my eager-initializing singleton class template.
template<typename T>
class singleton_wrapper {
singleton_wrapper();
singleton_wrapper(singleton_wrapper const &);
singleton_wrapper & operator = (singleton_wrapper const &);
static T instance;
template<typename T> friend T & singleton();
};
template<typename T> T singleton_wrapper<T>::instance;
template<typename T>
T & singleton() {
return singleton_wrapper<T>::instance;
}
上课...
class foo {
public:
void bar() { }
};
...一个人可以使用以下内容访问它的单个实例(在main()之前初始化):
...One would access a single instance of it (initialized before main()) using the following:
singleton<foo>().bar();
再次感谢您的帮助,尤其是GMan.我对堆栈的第一次体验感到非常满意溢出.
Thanks again for the help, especially GMan. I'm very pleased with my first experience on stackoverflow.
推荐答案
您不能,因为您没有具体实例.您可能需要创建一个可以引用的实际实例:
You can't, since you don't have a concrete instance. You can need to create an actual instance that you can refer to:
template <class T>
class singleton {
...
private:
static T instance_;
public:
static T& instance;
};
template <class T> T singleton<T>::instance_;
template <class T> T& singleton<T>::instance = singleton<T>::instance;
或者,更简单地说,就是完全放弃参考文献:
Or, more simply, just ditch the reference altogether:
template <class T>
class singleton {
...
public:
static T instance;
};
template <class T> T singleton<T>::instance;
这篇关于初始化"T&"类型的静态成员的正确方法是什么在模板化的类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!