简体   繁体   English

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

[英]Need of privatizing assignment operator in a Singleton class

Can someone justify the need of privatizing the assignment operator in a Singleton class implementation?有人可以证明在 Singleton class 实现中私有化赋值运算符的必要性吗?

What problem does it solve by making Singleton& operator=(Singleton const&);通过使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
};

Assignment on a singleton is simply a nonsense operation since only one object of it should ever exist.对 singleton 的赋值只是一个无意义的操作,因为它应该只存在一个 object。

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.

There's only one singleton.只有一个 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.您需要件事才能使副本保持正常,大多数副本操作员需要检查self==&other以确保安全。

This private trick is a hack.这个private技巧是一个黑客。 C++0x does it better. C++0x 做得更好。

Begin rant...开始吐槽...

IMHO a Singleton is a contradiction in terms.恕我直言,Singleton 在术语上是矛盾的。 It's a product of the silly idea that everything must be an object in order to be encapsulated.这是一个愚蠢想法的产物,所有东西都必须是 object 才能被封装。 It's the same brainache that bore Java's Math.sin(x) et al.这与 Java 的Math.sin(x)等人的脑痛相同。

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. singleton 的任何私有“成员”都可以隐藏在 .cpp 中的匿名命名空间中。 Encapsulation achieved, and you don't have that cumbersome extra syntax.实现了封装,并且您没有那种繁琐的额外语法。

MyNamespace :: foo ();

instead of代替

MyClass :: instance () .foo ();

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.赋值运算符访问说明符无关紧要,因为无论如何都不可能使用它。

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.声明一个私有赋值运算符只是符合人们的期望。

When you use a singleton the reason you implement it is because you only want one instance of an object of that class.当您使用 singleton 时,您实施它的原因是因为您只需要该 class 的 object 的一个实例。 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.复制构造函数也是如此。

My reasoning is this: if only one instance may be around, operator= could be defined without problem, since it will not do anything significant.我的理由是:如果只有一个实例,那么 operator= 可以毫无问题地定义,因为它不会做任何重要的事情。 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.顺便说一句,同样的推理也适用于析构函数。

Inherit boost::noncopyable (privately) in singleton class pattern than to define private copy construction and assignment operator.在 singleton class 模式中继承 boost::noncopyable(私有)而不是定义私有复制构造和赋值运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM