![]() |
Can singleton be owned by any object other than itself
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. |
Re: Can singleton be owned by any object other than itself
On Jun 20, 3:42*am, tech <naumansulai...@googlemail.com> 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 |
Re: Can singleton be owned by any object other than itself
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. |
Re: Can singleton be owned by any object other than itself
tech <naumansulaiman@googlemail.com> 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__ |
Re: Can singleton be owned by any object other than itself
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 |
Re: Can singleton be owned by any object other than itself
On Jun 20, 4:27*am, tech <naumansulai...@googlemail.com> 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 |
Re: Can singleton be owned by any object other than itself
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/> |
Re: Can singleton be owned by any object other than itself
On Jun 21, 12:30*am, "Daniel T." <danie...@earthlink.net> wrote:
> tech <naumansulai...@googlemail.com> 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? |
| All times are GMT. The time now is 10:52 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.