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 4 5707
"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
"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
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
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. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
8 posts
views
Thread by Ernst Murnleitner |
last post: by
|
11 posts
views
Thread by Roger Leigh |
last post: by
|
15 posts
views
Thread by Samee Zahur |
last post: by
|
13 posts
views
Thread by Adam H. Peterson |
last post: by
|
7 posts
views
Thread by Sunny |
last post: by
|
2 posts
views
Thread by superseed |
last post: by
|
11 posts
views
Thread by Kevin Prichard |
last post: by
|
8 posts
views
Thread by Per Bull Holmen |
last post: by
|
4 posts
views
Thread by Josefo |
last post: by
| | | | | | | | | | |