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

Home Posts Topics Members FAQ

Singleton question

JFR
i found this code sample on the net that i wanted to use:


class CUniqueObject
{
private:
CUniqueObject( void ) : m_iValue(0) { }
~CUniqueObject( void ) { }
public:
void SetValue( int iValue ) { m_iValue = iValue; }
int GetValue( void ) { return m_iValue; }

static CUniqueObject *GetInstance( void )
{
if( m_pSingleton == 0 )
{
std::cout << "creating singleton." << std::endl;
m_pSingleton = new CUniqueObject;
}
else
{
std::cout << "singleton already created!" << std::endl;
}

return m_pSingleton;
}

static void Kill( void )
{
if( m_pSingleton != 0 )
{
delete m_pSingleton;
m_pSingleton = 0;
}
}
private:
int m_iValue;
static CUniqueObject *m_pSingleton;
};
CUniqueObject *CUniqueObject: :m_pSingleton = 0;
int main( int argc, char *argv[] )
{

CUniqueObject *pObj1, *pObj2;
pObj1 = CUniqueObject:: GetInstance();
pObj2 = CUniqueObject:: GetInstance();
pObj1->SetValue( 11 );
std::cout << "pObj1::m_iValu e = " << pObj1->GetValue() << std::endl;
std::cout << "pObj2::m_iValu e = " << pObj2->GetValue() << std::endl;
pObj1->Kill();

return 0;
}
when i run the above code and debug it, the pObj1 shows that it
contains 2 things, a m_iValue and a m_pSingleton. However, the
m_pSingleton contains a CUniqueObject* which in turns also contains a
m_iValue and a m_pSingleton, and this goes on and on. I can expand the
object trees in the debugger infinitely.
At first i thought there was a problem with the code, but i think it's
just the debugger's way of showing objects.

Is this behavior "normal"?

Jul 23 '05
10 1509
Lionel wrote:
Torsten Mueller wrote:
In my experience the singleton pattern is normally enforced by people
having found this just a really gorgeous idea and not knowing real
problems.

I have had some very good uses for the Singleton pattern in the past.
Admittedly they were in Java which does have some consequences.

I can't think of a situation where you would use the Singleton pattern
and would want to have subclasses, that just sounds crazy IMHO. The idea
generally is that you only want one instance of some set of data that is
operated on by everyone, this way it stays consistent.

I have also never gone looking to just apply a design patter for the fun
of it, I have always started with a problem and just discovered that a
particular pattern is suitable for purpose.


Actually, I possibly didn't read your post carefully enough,
particularly your discussion of protected instead of private. I would
agree on that!
Jul 23 '05 #11

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

Similar topics

1
2038
by: Richard A. DeVenezia | last post by:
foo() generates elements with event handlers that invoke foo function properties. Is this an abhorrent or misthought pattern ? It allows just the one occurence of identifier /foo/ to be changed to /whatever/ when need arises and everything should still work. function foo () { var callee = arguments.callee
4
8243
by: Eric | last post by:
Perhaps this question has been posed before (I'd be surprised if it hasn't) but I just gotta know... Is it possible to combine the Singleton and Factory Method design patterns in the same class? Let's start with your basic Singleton class: class Singleton {
10
5310
by: ferdinand.stefanus | last post by:
Hi Could someone tell me what's the difference between these two singleton implementations: First implementation: class Singleton { public:
1
1413
by: Stephen Brown | last post by:
I posted a question yesterday about a Singleton I have, using the standard dotNet pattern, that seems to get recreated several times a day. I turned on my performance counters (thanks to Mickey William's post yesterday) and my Singleton restarting happens at the same time as an application restart. Now it's pretty clear it has nothing to do with garbage collection and it makes sense that application restart would destroy my singleton. ...
21
2469
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
14
3034
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is can a singleton class have a number of paramterised constructors enabling me to pass in parameters or not? I am trying to use the following to send in a parmeter to a constructor, but getting an error with it. I have a feeling that I am not...
2
6348
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any kind of inheritance: singletonObj = new function() { this.prop = true; } Here are two ways to create a singleton with inheritance:
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; }
2
1590
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates" I mean classes for which terrible problems would clearly arise if more than one instance were to exist. But as I'm getting into the design of this new solution, I'm realizing that a large percentage of the classes _could be_ implemented as...
0
9481
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
10336
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...
0
9953
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
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...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.