需要在 Singleton 类中私有化赋值运算符

发布于 2024-11-19 19:03:21 字数 473 浏览 0 评论 0原文

有人可以证明在 Singleton 类实现中私有化赋值运算符的必要性吗?

通过Singleton&来解决什么问题?运算符=(Singleton const&); 私有?

class Singleton {
public:
  static Singleton& Instance() {
    static Singleton theSingleton;
    return theSingleton;
  }

private:
  Singleton(); // ctor hidden
  Singleton(Singleton const&); // copy ctor hidden
  Singleton& operator=(Singleton const&); // assign op. hidden
  ~Singleton(); // dtor hidden
};

Can someone justify the need of privatizing the assignment operator in a Singleton class implementation?

What problem does it solve by making Singleton& operator=(Singleton const&); private?

class Singleton {
public:
  static Singleton& Instance() {
    static Singleton theSingleton;
    return theSingleton;
  }

private:
  Singleton(); // ctor hidden
  Singleton(Singleton const&); // copy ctor hidden
  Singleton& operator=(Singleton const&); // assign op. hidden
  ~Singleton(); // dtor hidden
};

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

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

发布评论

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

评论(7

冷情妓 2024-11-26 19:03:21

对单例进行赋值只是一种无意义的操作,因为它只应该存在一个对象。

将赋值运算符设置为私有有助于诊断如下无意义代码:

Singleton& a = Singleton::Instance();
Singleton& b = Singleton::Instance();
a = b; // Oops, accidental assignment.

Assignment on a singleton is simply a nonsense operation since only one object of it should ever exist.

Making the assignment operator private helps diagnose nonsense code such as the following:

Singleton& a = Singleton::Instance();
Singleton& b = Singleton::Instance();
a = b; // Oops, accidental assignment.
乙白 2024-11-26 19:03:21

如果您只需要一个实例,则复制构造函数应该是私有的。赋值运算符访问说明符并不重要,因为无论如何都无法使用它。

If you only want one instance, the copy constructor should be private. The assignment operator access specifier does not matter, because it will be impossible to use anyway.

Hello爱情风 2024-11-26 19:03:21

只有一个单身人士。复制它是没有意义的。为了保证副本的正常运行,您需要两件事,并且大多数复制操作员需要检查self==&other以确保安全。

这个private技巧是一种黑客行为。 C++0x 做得更好。

开始咆哮。 ..

恕我直言,单例是一个矛盾的术语。这是一个愚蠢想法的产物,即一切都必须是一个对象才能被封装。这与 Java 的 Math.sin(x) 等人的头痛是一样的。

如果“单例”只是命名空间中的一组自由函数,那么您的生活会更简单。单例的任何私有“成员”都可以隐藏在 .cpp 中的匿名名称空间中。实现了封装,并且您无需再使用繁琐的额外语法。

MyNamespace :: foo ();

而不是

MyClass :: instance () .foo ();

There's only one singleton. It makes no sense to copy it. You need two things for a copy to be sane and most copy operators need to check for self==&other in order to be safe.

This private trick is a hack. C++0x does it better.

Begin rant...

IMHO a Singleton is a contradiction in terms. It's a product of the silly idea that everything must be an object in order to be encapsulated. It's the same brainache that bore Java's Math.sin(x) et al.

Your life will be simpler if the "singleton" is simply a set of free functions in a namespace. Any private "members" of the singleton can be hidden in an anonymous namespace in the .cpp. Encapsulation achieved, and you don't have that cumbersome extra syntax.

MyNamespace :: foo ();

instead of

MyClass :: instance () .foo ();
源来凯始玺欢你 2024-11-26 19:03:21

将赋值运算符设为私有并不会真正改变任何东西,
因为您需要两个实例才能分配。确实对应
人们期望看到的内容;通常,如果副本
构造函数是私有的,赋值运算符也是私有的。声明一个
私有赋值运算符正好符合人们的期望。

Making the assignment operator private doesn't really change anything,
since you need two instances to be able to assign. It does correspond
to what people may expect to see; it's usual that if the copy
constructor is private, the assignment operator is as well. Declaring a
private assignment operator simply corresponds to people's expectations.

春庭雪 2024-11-26 19:03:21

当您使用单例时,实现它的原因是因为您只需要该类的对象的一个​​实例。换句话说,无需创建实例的副本,因为您只能拥有一个实例。复制构造函数也是如此。

When you use a singleton the reason you implement it is because you only want one instance of an object of that class. In other words there is no need to make a copy of the instance because you can only have one instance. It is the same for the copy constructor.

终难愈 2024-11-26 19:03:21

我的推理是这样的:如果只有一个实例,则可以毫无问题地定义operator=,因为它不会做任何重要的事情。如果我们将其设为私有,编译器将通过将任何使用该运算符的尝试标记为错误来增加一层安全性。

顺便说一句,同样的推理也适用于析构函数。

My reasoning is this: if only one instance may be around, operator= could be defined without problem, since it will not do anything significant. if we make it private, the compiler will add one more level of safety by flagging any attempt to use that operator as an error.

The same reasoning holds for the destructor, by the way.

白衬杉格子梦 2024-11-26 19:03:21

在单例类模式中继承 boost::noncopyable (私有),而不是定义私有复制构造和赋值运算符。

Inherit boost::noncopyable (privately) in singleton class pattern than to define private copy construction and assignment operator.

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