BartVandewoestyne / Design-Patterns-GoF

Working code for the Design Patterns book from the Gang of Four.
278 stars 122 forks source link

Registry of Singletons: do not allow the creation of MySingleton instances using default constructor #6

Open BartVandewoestyne opened 1 year ago

BartVandewoestyne commented 1 year ago

Apparently, in the Registry of Singletons example, it is still possible to create instances of MySingleton using its default constructor. If we try to avoid that by making the MySingleton() constructor protected instead of public, then the line

static MySingleton theSingleton;

in MySingleton.cpp gives an error at compile time:

error C2248: 'MySingleton::MySingleton': cannot access protected member declared in class 'MySingleton'

See also the TODO in Creational_Patterns/Singleton/registry_of_singletons/main.cpp.