Need of privatizing assignment operator in a Singleton class

前端 未结 7 946
一生所求
一生所求 2021-01-18 04:01

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

What problem does it solve by making Singleton& o

相关标签:
7条回答
  • 2021-01-18 04:48

    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.
    
    0 讨论(0)
提交回复
热议问题