初始化“T &”类型的静态成员的正确方法是什么?在模板类中?

发布于 2024-08-18 06:21:47 字数 1982 浏览 7 评论 0原文

我正在玩一个急于初始化的通用单例类。这个想法是,您可以像这样公开地继承该类:

class foo : public singleton<foo> { };

我在这个过程中学到了很多东西,但我现在陷入困境,因为它破坏了我的 Visual Studio 2008 链接器。问题在于静态实例成员和/或其初始化。

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;

当我尝试这样做时...

class foo : public singleton<foo> { };

我收到此错误...

错误 C2248: 'singleton::singleton' : 无法访问私有成员 在“singleton”类中声明

...

此诊断发生在编译器生成的函数“foo::foo(void)”中

我的解释是,单例想要构造一个 foo 对象,通过继承,该对象依赖于单例的构造,该单例的构造构造函数是私有的。我认为单例可以访问它自己的构造函数,但我想不能。有什么想法吗?

编辑2:

我意识到从 singleton 继承的方法存在需要更改类以用作单例的问题。我最终为我的热切初始化单例类模板提供了以下代码。

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() 之前初始化):

singleton<foo>().bar();

再次感谢您的帮助,尤其是 GMan。我对堆栈溢出的第一次体验感到非常满意。

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> { };

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;

Any insight would be greatly appreciated!

EDIT:

With this class declaration...

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> { };

I get this error...

error C2248: 'singleton::singleton'
: cannot access private member
declared in class 'singleton'

...

This diagnostic occurred in the compiler generated function 'foo::foo(void)'

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?

EDIT 2:

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;
}

For class...

class foo {
public:
    void bar() { }
};

...One would access a single instance of it (initialized before main()) using the following:

singleton<foo>().bar();

Thanks again for the help, especially GMan. I'm very pleased with my first experience on stackoverflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-08-25 06:21:47

你不能,因为你没有具体的实例。您可能需要创建一个可以引用的实际实例:

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;

或者,更简单地说,完全放弃引用:

template <class T>
class singleton {
    ...
public:
    static T instance;
};
template <class T> T singleton<T>::instance;

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;
葬シ愛 2024-08-25 06:21:47

即兴:将实例更改为“T”类型而不是“T &”。

Off-the-sleeve: change instance to be of type 'T' instead of 'T &'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文