Can singleton be owned by any object other than itself

T

tech

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.
 
G

Greg Herlihy

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
 
T

tech

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.
 
P

Pascal J. Bourguignon

tech said:
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.
 
M

Marcel Müller

tech said:
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
 
G

Greg Herlihy

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
 
D

Daniel Pitts

tech said:
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.
 
D

Diego Martins

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top