473,545 Members | 529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6111

"baumann@pa n" <ba*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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@pa n" <ba*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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*********@gm ail.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
1887
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 (factory) class Fa. I wanted to make that only F can call new A
11
4577
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member...
15
6565
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local...
13
7687
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. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For...
7
1868
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 constructor is triggered by the first of the following events to occur within an application domain: - An instance of the class is created.
2
2644
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 following type : Exception, Error, Warning, Infos (with differents colors, etc). I would like to use it like the Console.WriteLine("My Message"); on...
11
3803
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
8
8913
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. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
4
3496
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 vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.