Re: Private Static member object of the same class
On Jun 7, 8:04 pm, Sashi <small...@gmail.com> wrote:
On Jun 7, 12:03 pm, "VSP" <a...@abc.com> wrote:
I am just implementing singleton pattern in various ways. In
one implementation I created a static member and returning
that static member in the getInstance() function. I have
made constructor as private and destructor as public.
class Single
{
private:
Single()
{
}
public:
~Single()
{
}
static Single& getInstance()
{
return s;
}
static Single s;};
Single Single::s;
You need to modify the getInstance() method to first create an object
and then return a pointer/reference to it.
Yes and no. If he never accesses the singleton from the
constructor or the destructor of another static object, this
solution works just fine.
Of course, it's just as easy to write a version in which such
accesses also work, and the most of the standard idioms support
them, or at least accesses from constructors of static objects.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34