473,401 Members | 2,139 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,401 software developers and data experts.

Singleton question.

Hi, all
I've written a program to produce singleton object.
Like this.

---------------------------------------------------
class A{
public:
static A* getInstance();
void print(); \\ print out 'a' I won't implement here.
void setA(int); \\ set 'a', I won't implement here.
~A(){}
protected:
A(){}
private:
int a;
static A* instance;
};

A* A::instance = NULL;

A* A::getInstance()
{
if(instance)
return instance;
else
return (instance=new A());
}

int main()
{
A * a = A::getInstance();
a->setA(10);
a->print();
delete a; // <-- This line is a question. Read below.

a->setA(15); // <-- This line should crash, isn't it?
a->print(); // <-- This line should crash, isn't it?

return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we've deleted that object (line 'delete a;')
Thank in advance.
Prawit Chaivong.

Jul 23 '05 #1
10 1221
> delete a; // <-- This line is a question. Read below.

a->setA(15); // <-- This line should crash, isn't it?
a->print(); // <-- This line should crash, isn't it?

return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we've deleted that object (line 'delete a;')

This is undefined behavior. One cannot expect anything definitive when
you access members after deleting an object. Its a different matter
that you _should not_ do that!

Regards,
Srini

pr******@gmail.com wrote: Hi, all
I've written a program to produce singleton object.
Like this.

---------------------------------------------------
class A{
public:
static A* getInstance();
void print(); \\ print out 'a' I won't implement here.
void setA(int); \\ set 'a', I won't implement here.
~A(){}
protected:
A(){}
private:
int a;
static A* instance;
};

A* A::instance = NULL;

A* A::getInstance()
{
if(instance)
return instance;
else
return (instance=new A());
}

int main()
{
A * a = A::getInstance();
a->setA(10);
a->print();
delete a; // <-- This line is a question. Read below.

a->setA(15); // <-- This line should crash, isn't it?
a->print(); // <-- This line should crash, isn't it?

return 0;
}
-----------------------------------------------------------
The result of this program is:
#./SingleTon
10
15
#
------------------------------------------------------------
My question is:
"Why instance of class A still exist? "
My understanding is we've deleted that object (line 'delete a;')
Thank in advance.
Prawit Chaivong.


Jul 23 '05 #2
What do you think if I prevent this object from deleting.
By declare Dtor as protected.

Jul 23 '05 #3
> What do you think if I prevent this object from deleting.

You can return a static reference instead of a pointer to the lone
object.

static A& A::getInstance(void)
{
static A instance;
return instance;
}

But this method will have implications in a multi-threaded application.

By declare Dtor as protected.


Destructors must NOT be made private! The compiler would not allow you
to do that.

Regards,
Srini

Jul 23 '05 #4
Srini wrote:
By declare Dtor as protected.


Destructors must NOT be made private! The compiler would not allow you
to do that.


Destructors can be protected or private, and the compiler
would allow it. It would be useful in exactly this case,
to prevent the main program from deleting the object.

Jul 23 '05 #5
> Destructors can be protected or private, and the compiler
would allow it. It would be useful in exactly this case,
to prevent the main program from deleting the object.


I stand corrected. Only if there's an object instance that needs to be
destroyed and the compiler finds the destructor is private, does it
throw up an error. Thanks Old Wolf!

Srini

Jul 23 '05 #6

"Srini" <sr*********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
What do you think if I prevent this object from deleting.
You can return a static reference instead of a pointer to the lone
object.

static A& A::getInstance(void)
{
static A instance;
return instance;
}

But this method will have implications in a multi-threaded application.


Can you tell about some implications plz?

By declare Dtor as protected.


Destructors must NOT be made private! The compiler would not allow you
to do that.

Regards,
Srini

Jul 23 '05 #7
> > But this method will have implications in a multi-threaded application.
Can you tell about some implications plz?


The version of the singleton implementation that returns a static
pointer, would use operator new to allocate the lone object. Because
operator new is thread-safe it can be safely used in a multithreaded
app. Moreover with the static pointer version, its our responsibility
to delete the lone object. But with the second version, it gets
destructed automatically when the app terminates.

Jul 23 '05 #8

I don't think a caller has responsibility to delete that object since
it might be used in somewhere else.

Jul 23 '05 #9
> I don't think a caller has responsibility to delete that object since
it might be used in somewhere else.


I did not mean the 'caller'. Whoever writes such a class must provide
some means for cleaning up.

Srini

Jul 23 '05 #10
You could assign the instance to a global or module-level auto_ptr (or
boost::scoped_ptr) to have it cleaned up automatically upon program
termination.

Regards
-Laurens
Jul 23 '05 #11

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

Similar topics

1
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...
4
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?...
10
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
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...
21
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...
14
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...
2
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...
6
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...
3
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() {...
2
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"...
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
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,...
0
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,...
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.