473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static member and private /protected constructor

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
4 6087

"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.