
Globalness of Singleton pattern
I doubt that globalness is real word, but here's my question:
The method espoused by Scott Meyers and others for having a single instance
of a class includes the following ideas:
class CMyClass
{
CMyClass(){}
public:
static CMyClass& MyClass();
Quote:
}
static CMyClass& CMyClass::MyClass()
{
static CMyClass myclass;
return myclass;
Quote:
}
My concern is how to adapt this method so that the instance of this class is
not globally available. Any rogue code maintainer (such as myself) in the
same namespace can call CMyClass::MyClass(). Can this method be adapted to
restrict access to the instance such that any user of the instance must get
it from a composing class?
class CGateKeeper
{
public:
m_MyClass* mineallmine;
CGateKeeper(){m_MyClass = &CMyClass::MyClass();}
Quote:
}
I think I should keep my 'almost globals' from roaming freely. Granted, the
above example doesn't use an accessor, but at least I can thus somewhat
group all my 'almost global' instances together.