有人可以證明需要在Singleton類實現中私有化賦值運算符嗎?需要在Singleton類中私有化賦值運算符
它通過使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 ='不是危險的,只是無稽之談。 –