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
};
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: