C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static member and protected/private constructor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
baumann.Pan@gmail.com
Guest





PostPosted: Wed Apr 20, 2005 3:47 pm    Post subject: static member and protected/private constructor Reply with quote



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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ali Çehreli
Guest





PostPosted: Thu Apr 21, 2005 8:39 am    Post subject: Re: static member and protected/private constructor Reply with quote



[email]baumann.Pan (AT) gmail (DOT) com[/email] wrote:

Quote:
- private; that is, its name can be used only by members and friends
of the class in which it is
declared.

[...]

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

[...]

Quote:
anyone could point out why I am wrong? thanks in advance.

You are wrong because you think that static members are not members of the
class. They are members too.

Quote:
class Singleton
{
public:
static Singleton& Instance ()
{
static Singleton theInstance;

Here, the static member function Singleton::Instance accesses the private
default constructor.

Quote:
return theInstance;
}


private:
Singleton () {}
};

Ali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gerhard Menzl
Guest





PostPosted: Thu Apr 21, 2005 8:56 am    Post subject: Re: static member and protected/private constructor Reply with quote



[email]baumann.Pan (AT) gmail (DOT) com[/email] wrote:

Quote:
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 () {}
};

Instance() *is* a member of Singleton, hence it has access to its
private constructor. Access control specifiers grant access to classes
and functions, not to individual objects.

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maciej Sobczak
Guest





PostPosted: Thu Apr 21, 2005 12:05 pm    Post subject: Re: static member and protected/private constructor Reply with quote

Hi,

[email]baumann.Pan (AT) gmail (DOT) com[/email] wrote:

Quote:
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.

That's right.

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

Instance() *should* have access to private constructor of the Singleton
class, because Instance is a member function of the Singleton class.

It is static, but it is a member. Your code and the rule you cited are
OK with each other.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.