Re: Private constructor
From: jeffc (nobody_at_nowhere.com)
Date: 12/15/03
- Next message: jeffc: "Re: enum question..."
- Previous message: Dan Cernat: "Re: Problem of Pass by Reference in C++"
- In reply to: Andy: "Private constructor"
- Next in thread: Andy: "Re: Private constructor"
- Reply: Andy: "Re: Private constructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 15 Dec 2003 11:31:40 -0500
"Andy" <garth_rockett@yahoo.com> wrote in message
news:96dbbc80.0312140737.32433ee3@posting.google.com...
> 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?
The point of making a constructor private is usually to NOT allow anyone to
use it. (Imagine that instead of the "private" keyword it was
"inaccessible".) That allows you to have a class available for use, but not
for anyone to just create one with a plain constructor. Normally you'd
provide some other means for creating one, where you can control whether it
gets created or not. For example, you could have a static function called
"create", and that would have to be called to get a new object. In the
create function, you can first check to see if you've created one before, by
keeping a flag or counter. If so, you don't create one. If not, you create
one and return one. That way no more than one can ever exist.
- Next message: jeffc: "Re: enum question..."
- Previous message: Dan Cernat: "Re: Problem of Pass by Reference in C++"
- In reply to: Andy: "Private constructor"
- Next in thread: Andy: "Re: Private constructor"
- Reply: Andy: "Re: Private constructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|