Singleton in Action?
Tony
tonytdominguez at aol.com
Sun Feb 3 02:55:36 UTC 2019
On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:
>
> So, my big question is, do I instantiate like this:
>
> DSingleton singleton = new DSingleton;
>
> Or like this:
>
> DSingleton singleton = singleton.get();
>
> And subsequent calls would be...? The same? Using get() only?
This seems to be a case where D's definition of "private" can
cause confusion versus examples from other languages with private
as "access only inside the class". In other languages
DSingleton singleton = new DSingleton;
would never compile (the desired behavior), since the constructor
is private.
But D allows it in some cases due to "private to class OR
module". If DSingleton is in the same module, as for example in a
small test program with DSingleton and main() in the same file,
then it compiles.
So I think your code is fine, it's just that it can be
circumvented - the private constructor can be called - if
DSingledton and the instantiating code are in the same module.
More information about the Digitalmars-d-learn
mailing list