static member and private /protected constructor

B

baumann@pan

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
 
S

Sharad Kala

baumann@pan said:
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
 
D

David White

baumann@pan said:
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
 
U

ulrich

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.
 
R

Rolf Magnus

baumann@pan said:
- 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top