Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can singleton be owned by any object other than itself

Reply
Thread Tools

Can singleton be owned by any object other than itself

 
 
tech
Guest
Posts: n/a
 
      06-20-2008
Is it ok to allow another object to own a singleton object, or is
this definitely a NO NO.
I have a utility class that i want to provide access to a whole group
of subobjects so i can make
this utility a singleton. But i was wondering if i can have it owned
by a top level object
which encompasses all the other subobjects(which would use the
singleton utility)
so there shouldn't be any refs to the handle floating around when the
top level is destroyed
and it would of course destroy the singleton.
 
Reply With Quote
 
 
 
 
Greg Herlihy
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 3:42*am, tech <(E-Mail Removed)> wrote:
> Is it ok to allow another object to own a singleton object, or is
> this *definitely a NO NO.
> I have a utility class that i want to provide access to a whole group
> of subobjects so i can make
> this utility a singleton. But i was wondering if i can have it owned
> by a top level object
> which encompasses all the other subobjects(which would use the
> singleton utility)
> *so there shouldn't be any refs to the handle floating around when the
> top level is destroyed
> and it would of course destroy the singleton.


In my experience, it's actually a very good idea to have a singleton
be a member variable of another singleton (whose life span encompasses
the life span of the member singleton). In this way, a C++ program can
ensure the orderly construction and destruction of its singletons.
Otherwise, it is much more difficult for a program to control the
order in which singletons are allocated and destroyed - making it much
more likely that a singleton will be destroyed before one of its
dependent singletons is destroyed.

Greg

 
Reply With Quote
 
 
 
 
tech
Guest
Posts: n/a
 
      06-20-2008
Thanks, but what i specifically would like to know if it is ok
for a singleton can be a member of a NON singleton object as
described in the case above.

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-20-2008
tech <(E-Mail Removed)> writes:

> Thanks, but what i specifically would like to know if it is ok
> for a singleton can be a member of a NON singleton object as
> described in the case above.


Think about it.

If you have a non-singleton class A, and you instanciate two objects
a1 and a2 of that class A, which one will be the owner of the
singleton object?

You can still have to pointers, from a1 and from a2, to the singleton
object, but a1 and a2 won't be owner.

Or if you insist, you could have one specific A instance be THE owner,
but again, you'll have to be extra careful that ALL the instances of A
be destroyed before the onwer A instance. Too much work, just forget
this owner/owned relationship.

--
__Pascal Bourguignon__
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      06-20-2008
tech wrote:
> Thanks, but what i specifically would like to know if it is ok
> for a singleton can be a member of a NON singleton object as
> described in the case above.


This is no singleton anymore, since at least as much instances exist as
there are instances of the non singleton class around.

A singleton should have a private constructor. This will prevent you
from doing bad things like that. The access is usually done by a factory
or a public static data member.
If a singleton's lifetime is managed by another class, this manager
class or a function of it should be a friend.


Marcel
 
Reply With Quote
 
Greg Herlihy
Guest
Posts: n/a
 
      06-20-2008
On Jun 20, 4:27*am, tech <(E-Mail Removed)> wrote:
> Thanks, but what i specifically would like to know if it is ok
> for a singleton can be a member of a NON singleton object as
> described in the case above.


I don't see how it would even be possible for a non-singleton class to
own a singleton class as a data member. Logically, a singleton class
object could be a data member only of another singleton class object
(whether the class happens to be called a singleton or not).
Otherwise, as a data member of more than one class object, the
singleton data member would cease to be a singleton.

Greg

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-20-2008
tech wrote:
> Is it ok to allow another object to own a singleton object, or is
> this definitely a NO NO.
> I have a utility class that i want to provide access to a whole group
> of subobjects so i can make
> this utility a singleton. But i was wondering if i can have it owned
> by a top level object
> which encompasses all the other subobjects(which would use the
> singleton utility)
> so there shouldn't be any refs to the handle floating around when the
> top level is destroyed
> and it would of course destroy the singleton.

The way I usually think about this is, do I really need a singleton, or
just a shared instance? Often, its is the latter. So, it makes sense
for the lifetime of the shared instance to be managed by the framework,
rather than itself. That makes it easier in the future if you find out
that you actually need exactly *two* instances. That way, the framework
can still own both instances, and each instance is shared.

So, yes it can be owned by an external object as long as the identity
and lifespan of the shared object is maintained appropriately.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Diego Martins
Guest
Posts: n/a
 
      06-23-2008
On Jun 21, 12:30*am, "Daniel T." <(E-Mail Removed)> wrote:
> tech <(E-Mail Removed)> wrote:
> > Is it ok to allow another object to own a singleton object, or is
> > this definitely a NO NO. I have a utility class that i want to
> > provide access to a whole group of subobjects so i can make this
> > utility a singleton. But i was wondering if i can have it owned by
> > a top level object which encompasses all the other subobjects(which
> > would use the singleton utility) so there shouldn't be any refs to
> > the handle floating around when the top level is destroyed and it
> > would of course destroy the singleton.

>
> class Foo {
> * *static Foo* instance;
> public:
> * *Foo() {
> * * * assert( instance == 0 );
> * * * instance = this;
> * *}
> * *~Foo() {
> * * * instance = 0;
> * *}
>
> };
>
> The above is a singleton that my boss uses quite a bit. I'm not a big
> fan of singletons (or globals in general) myself.


but where is the access point?
 
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
Any other testing providers other than Prometric vnapoli Microsoft Certification 0 11-09-2011 09:42 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
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
auto_ptr: reassigning to member of owned object Joost Ronkes Agerbeek C++ 2 10-03-2003 06:38 AM



Advertisments