Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Singleton question.

Reply
Thread Tools

Singleton question.

 
 
prawit.c@gmail.com
Guest
Posts: n/a
 
      05-31-2005
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.

 
Reply With Quote
 
 
 
 
Srini
Guest
Posts: n/a
 
      05-31-2005
> 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



http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
 
prawit.c@gmail.com
Guest
Posts: n/a
 
      05-31-2005
What do you think if I prevent this object from deleting.
By declare Dtor as protected.

 
Reply With Quote
 
Srini
Guest
Posts: n/a
 
      05-31-2005
> 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

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-31-2005
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.

 
Reply With Quote
 
Srini
Guest
Posts: n/a
 
      05-31-2005
> 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

 
Reply With Quote
 
Usenet
Guest
Posts: n/a
 
      05-31-2005

"Srini" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
Srini
Guest
Posts: n/a
 
      05-31-2005
> > 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.

 
Reply With Quote
 
prawit.c@gmail.com
Guest
Posts: n/a
 
      05-31-2005

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

 
Reply With Quote
 
Srini
Guest
Posts: n/a
 
      06-01-2005
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments