473,785 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton class with static member variables

Hi,

I have come across singleton class with some member variables are
declared as static with public scope.
As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.

Jul 23 '05 #1
5 4716
On 15 Jun 2005 03:44:19 -0700, "LinuxGuy" <ra**********@g mail.com>
wrote:
Hi,

I have come across singleton class with some member variables are
declared as static with public scope.
As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.


Well, the function used to return the singleton instance (usually
called "getInstanc e()" or something similar, and usually returning a
pointer to the singleton instance) MUST be static -- can you figure
out why? For similar reasons, there is often a static "unload()" or
"destroy()" function.

Otherwise, I would assume that other static functions are there for
the same reason we have static functions in any non-singleton class.
One of the more important ones might be that you cannot assign the
address of a non-static member function to a regular function pointer.

--
Bob Hairgrove
No**********@Ho me.com
Jul 23 '05 #2
ya Thanks for help..

Jul 23 '05 #3
The normal usage of a singleton is that it is constructed using a
getInstance() function with some code like
if ( NULL == pInst )
{ Create/Initialise }

return pInst;

Of course the first time you call getInstance, the object doesn't
exist. Obviously the code "exists", but not the data.
Static functions can be called, even if an object of that class has not
been created. To protect you, it's not possible to access data members
because they may not have been set up.

However you may access static data, because well umm err it's static,
and it's life cycle is independant of the class it is referenced in.
Thus it exists typically for the life of the program.
Note that you declare class statics outside the class so they are not
much different from globals, but with more limited visibility.

DominiConnor
Quant Headhunter

Jul 23 '05 #4
On Wed, 15 Jun 2005 14:44:19 +0400, LinuxGuy <ra**********@g mail.com>
wrote:
I have come across singleton class with some member variables are
declared as static with public scope.
As singleton class always return only one instance. ie.
single copy of object is maintained all the time. can someone tell me
the reason behind declaring those variables as static one.


It might be that is not a singleton rather monostate pattern, that is you
can create as many objects of it as you like and yet they all share the
same state.

--
Maxim Yegorushkin
Jul 23 '05 #5
On Wed, 15 Jun 2005 16:06:01 +0400, <bi****@math.co m> wrote:
The normal usage of a singleton is that it is constructed using a
getInstance() function with some code like
if ( NULL == pInst )
{ Create/Initialise }

return pInst;


I don't think there exist such a thing as "normal usage of a singleton". I
can, for example, have a header file with a constant pointer guarantied to
be initialized with an address of a valid object through the lifetime of
my program, and this is also a model of singleton concept. IMO, it's a
common misconception to think that a singleton must have a static
getInstance() member function. Anything that behaves as singleton is a
singleton.

--
Maxim Yegorushkin
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3915
by: yccheok | last post by:
hello, in a singleton design, i trying to make my parent class having protected constructor and destructor. however, the compiler give me error when my child, trying to delete itself through parent pointer. isn't child should have access to parent protected destructor? thank you. class CMachineFactory
0
1727
by: Tony Wong | last post by:
I am trying to implement the Singleton Pattern for a assembly (class) which control a common resource on my computer. I need the Singleton behavior within a single process which contain multiple AppDomains. I use a static member to count class instance like this, public __gc class foo: public IDisposable { public:
6
2279
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class Singleton { static Singleton s; int i; Singleton(int x) : i(x) { }
12
1895
by: keepyourstupidspam | last post by:
Hi, I am writing a windows service. The code runs fine when I start the service when my machine is running but it fails to start automatically when the machine reboots. The code bombs out when it reaches code that tries to access a singleton class. This is the code.
6
3198
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the singleton is initialized as a static variable , it seems there is some threading issue . Is it the issue during singleton initialization only , or during the access also? If the singleton is per thread basis (then no more singleton though ), and...
3
435
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: Singleton * instance() { if(pSingleton==NULL) pSingleton = new Singleton; return pSingleton; }
3
18253
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
3
2815
by: koredump | last post by:
I have inherited a Business Object architecture that makes heavy use of the Singleton design patter. For example the data access layer class is implemented as a static Singleton "object", there are also other business objects logic and security supporting classes that are all implemented using the Singleton patter. I 'm attempting to make use of this class library in a webservice application. The problem is that some of the Singleton...
1
1500
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm getting a little confused here. I have a C# class that I'm trying to translate to VB. The C# class is essentially: public static class Class1 { ..... some private static variables and public static properties. } The translation (from several different translation programs) is:
0
9643
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.