By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

static member and private /protected constructor

P: n/a
hi all,

according the private / protected access control,
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.
- protected; that is, its name can be used only by members and
friends of the class in which it is
declared, and by members and friends of classes derived from this class
(see 11.5).

how can the codes below work correctly since the static function
Instance() should not have the accesss to private constructor
Singleton(); moreover, the statement static Singleton theInstance would
call the private constructor Singleton() to create the instance of the
class Singleton, but the static variable is not the members or friends
of the class Singleton, so the static variable could not access the
private constructor of the class Singleton.
anyone could point out why I am wrong? thanks in advance.


class Singleton
{
public:
static Singleton& Instance ()
{
static Singleton theInstance;
return theInstance;
}
private:
Singleton () {}
};

baumann@pan

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hi all,

according the private / protected access control,
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.
- protected; that is, its name can be used only by members and
friends of the class in which it is
declared, and by members and friends of classes derived from this class
(see 11.5).

how can the codes below work correctly since the static function
Instance() should not have the accesss to private constructor


Instance is a static *member function*, so it should have access to the
private constructor.

Sharad
Jul 23 '05 #2

P: n/a
"baumann@pan" <ba*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hi all,

according the private / protected access control,
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.
- protected; that is, its name can be used only by members and
friends of the class in which it is
declared, and by members and friends of classes derived from this class
(see 11.5).

how can the codes below work correctly since the static function
Instance() should not have the accesss to private constructor
Singleton(); moreover, the statement static Singleton theInstance would
call the private constructor Singleton() to create the instance of the
class Singleton, but the static variable is not the members or friends
of the class Singleton, so the static variable could not access the
private constructor of the class Singleton.

anyone could point out why I am wrong? thanks in advance.
The static function Instance is a member of class Singleton, so it is
allowed access to the private constructor.

class Singleton
{
public:
static Singleton& Instance ()
{
static Singleton theInstance;
return theInstance;
}
private:
Singleton () {}
};


DW
Jul 23 '05 #3

P: n/a
On 19 Apr 2005 23:41:47 -0700, baumann@pan <ba*********@gmail.com> wrote:
hi all,

according the private / protected access control,
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.
- protected; that is, its name can be used only by members and
friends of the class in which it is
declared, and by members and friends of classes derived from this class
(see 11.5).

how can the codes below work correctly since the static function
Instance() should not have the accesss to private constructor
it is a member function, so it _has_ access to any other members

Singleton(); moreover, the statement static Singleton theInstance would
call the private constructor Singleton() to create the instance of the
class Singleton, but the static variable is not the members or friends
theInstance does not call the constructor, it is created by the call to
the constructor Singleton, and this call, in turn is done by Instance(),
which, again, is a member of the class and thus can use any other members.

of the class Singleton, so the static variable could not access the
private constructor of the class Singleton.
anyone could point out why I am wrong? thanks in advance.


class Singleton
{
public:
static Singleton& Instance ()
{
static Singleton theInstance;
return theInstance;
}
private:
Singleton () {}
};

baumann@pan


Jul 23 '05 #4

P: n/a
baumann@pan wrote:
- private; that is, its name can be used only by members and friends
of the class in which it is declared.

how can the codes below work correctly since the static function
Instance() should not have the accesss to private constructor
Singleton();
This would be a contradiction. Above, you say members and friends of the
class in which it's declared should have access, here you say that a member
of the class should not have access.
moreover, the statement static Singleton theInstance would
call the private constructor Singleton() to create the instance of the
class Singleton, but the static variable is not the members or friends
of the class Singleton,
The static variable is just an instance of the class. The function
Instance() is who creates that, i.e. calls the constructor. Since it has
access, it can define variables of that type.
so the static variable could not access the private constructor of the
class Singleton.


The static variable doesn't exist before the constructor isn't called. So
you cannot logically say that it is the one accessing the constructor.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.