シングルトンクラスの実装で代入演算子を民営化する必要性を正当化できる人はいますか?シングルトンクラスの代入演算子の民営化の必要性
Singleton& operator=(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
};
開発者がa = bを実行してもわかります。両方のオブジェクトがSingletonの同じ静的インスタンスを指しているので、害はありません。したがって、民営化や代入演算子は、シングルトンが期待どおりに動作するための必須条件ではありません。 –
@PrashanthGNこれはノーオペレーションでなければならないのは正しいです。そうすれば 'operator ='は危険ではなく、ナンセンスになります。 –