Re: Private constructor

From: Matej Pivoluska (spam_at_spam.com)
Date: 12/14/03


Date: Sun, 14 Dec 2003 17:10:49 +0100

Andy wrote:

> 1) Is there any use of defining a class with a single constructor
> declared in private scope? I am not asking a about private copy
> constructors to always force pass/return by reference.
>
> 2) Is this in any way used to create singletons. Can someone say how?

If you want to create singletons, you have to declare all your constructors
(and operator=, too) as private.

Then you create a public function that checks if singleton was created
(through some static bool member) and then calls (private) constructor.

Then (if check passed) your function returns refernece (or pointer) on your
singleton and set true flag to static member -- singleton was created.

-- 
mP
http://pivoluska.matfyz.cz/


Relevant Pages

  • Re: Ada Singleton Pattern
    ... >> The public view of Singleton is unconstrained and limited, ... package Singletons is ... but you can declare it as either a constant or a function. ...
    (comp.lang.ada)
  • Re: Trigger CAPS LOCK ON When Wk Bk is Opened?
    ... Private Type OSVERSIONINFO ... Private Declare Function GetVersionEx Lib "kernel32" _ ... Public Sub CapsOn() ... Dim ScrollLockState As Boolean ...
    (microsoft.public.excel.programming)
  • Re: cant use udt in a class?
    ... Private Sub Command1_Click ... Another quirk is that you can declare Properties in a .BAS module, ... So you could actually expose functions from modules and hide the ... This would be essential when you went to exposing these modules in libraries ...
    (microsoft.public.vb.general.discussion)
  • Re: Types of constructors
    ... Private mUID As String ... Private Sub New ... the function had full access to the new object's private member fields. ... : What are the other kinds of constructors besides: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Updates to a single class instance
    ... I tried to use a singleton, ... public class Collector{ ... private static Collector ref; ...
    (comp.lang.java.programmer)