C++:模板和单例模式

发布于 2024-11-02 08:23:11 字数 487 浏览 0 评论 0原文

碰巧我需要臭名昭著的单例模式。更好的是,它发生了,所以我需要臭名昭著的 C++ 模板与该模式的结合。所以,令我困扰的是:

template <class T>
class PDatabaseTable
{
    ...

    static PDatabaseTable <T> & instance()
    {
        static PDatabaseTable <T> singleton;
        return singleton;
    }

    ...
};

这是实现单例的典型方法,该单例应该在第一次使用时创建。现在,我们有一个静态变量singleton。由于 instance() 函数可能会从多个不同的模块调用,因此问题是:对于任何给定类型 T 是否只有一个对象实例,还是每个模块实例化它自己的单例?

It happens so that I have a need of the infamous singleton pattern. Better yet, it happens so that I have a need of infamous C++ templates in combination with that pattern. So, what troubles me is this:

template <class T>
class PDatabaseTable
{
    ...

    static PDatabaseTable <T> & instance()
    {
        static PDatabaseTable <T> singleton;
        return singleton;
    }

    ...
};

This is a typical way to implement a singleton that's supposed to be created on the first use. Now, here we have a static variable singleton. Since the instance() function may be called from several different modules, the question is: will there be only one instance of the object for any given type T, or will every module instantiate its very own singleton?

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

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

发布评论

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

评论(4

七婞 2024-11-09 08:23:11

每种类型 T 都只有一个实例,就像如果它不是模板,就只有一个实例一样。

该函数是内联的,这意味着虽然它可以在多个编译单元中定义,但链接后将只有一个版本,并且任何本地静态对象都只有一个实例。

There will only be one instance for each type T, just as, if it weren't a template, there would only be one instance.

The function is inline, meaning that although it can be defined in multiple compilation units, after linking there will be only one version of it, and only one instance of any local static objects.

想挽留 2024-11-09 08:23:11

您的单例称为 Meyers Singleton,您可以在 G++ 中的静态局部变量和线程安全 文章很好地解释了静态局部变量如何线程安全地创建。

Your singleton is called Meyers Singleton and you can find an explanation about thread safety of this singleton type in Static locals and threadsafety in g++ article which nicely explains how static local variables are thread-safe to create.

请你别敷衍 2024-11-09 08:23:11

肯定会有只有一个实例

我只是想知道为什么不能将静态对象从函数中移到类主体中?

template <class T>
class PDatabaseTable
{
  static PDatabaseTable <T> singleton;
  static PDatabaseTable <T> & instance()
  {
    return singleton;
  }
};
template<class T>
PDatabaseTable<T> PDatabaseTable<T>::singleton;

Definitely there will be only one instance.

I am just wondering why can't you move that static object out of function to the class body ?

template <class T>
class PDatabaseTable
{
  static PDatabaseTable <T> singleton;
  static PDatabaseTable <T> & instance()
  {
    return singleton;
  }
};
template<class T>
PDatabaseTable<T> PDatabaseTable<T>::singleton;
蓝天 2024-11-09 08:23:11

您可以将 static 的 init 移到类体之外,这对于 static 函数也是可能的。

template <typename T>
class Singleton
{
public:
  static Singleton<T>* Singleton::getInstance();
  T* getMember() { member_; }
protected:
  Singleton() { member_ = new T; }
  ~Singleton() { if (singleton_) delete member_; }
private:
  static Singleton<T>* singleton_;
  T* member_;
};
template <typename T>
Singleton<T>* Singleton<T>::getInstance()
{
  if (NULL == singleton_) singleton_ = new Singleton;
  return singleton_;
}
template <typename T>
Singleton<T>* Singleton<T>::singleton_ = NULL;

You CAN move the init of the static outside of the class body, and this is also possible for the static function.

template <typename T>
class Singleton
{
public:
  static Singleton<T>* Singleton::getInstance();
  T* getMember() { member_; }
protected:
  Singleton() { member_ = new T; }
  ~Singleton() { if (singleton_) delete member_; }
private:
  static Singleton<T>* singleton_;
  T* member_;
};
template <typename T>
Singleton<T>* Singleton<T>::getInstance()
{
  if (NULL == singleton_) singleton_ = new Singleton;
  return singleton_;
}
template <typename T>
Singleton<T>* Singleton<T>::singleton_ = NULL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文