Can singleton be owned by any object other than itself

Discussion in 'C++' started by tech, Jun 20, 2008.

  1. tech

    tech Guest

    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.
    tech, Jun 20, 2008
    #1
    1. Advertising

  2. tech

    Greg Herlihy Guest

    On Jun 20, 3:42 am, 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.


    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
    Greg Herlihy, Jun 20, 2008
    #2
    1. Advertising

  3. tech

    tech Guest

    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.
    tech, Jun 20, 2008
    #3
  4. tech <> 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__
    Pascal J. Bourguignon, Jun 20, 2008
    #4
  5. 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
    Marcel Müller, Jun 20, 2008
    #5
  6. tech

    Greg Herlihy Guest

    On Jun 20, 4:27 am, 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.


    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
    Greg Herlihy, Jun 20, 2008
    #6
  7. tech

    Daniel Pitts Guest

    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/>
    Daniel Pitts, Jun 20, 2008
    #7
  8. On Jun 21, 12:30 am, "Daniel T." <> wrote:
    > 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.

    >
    > 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?
    Diego Martins, Jun 23, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tolu45
    Replies:
    4
    Views:
    4,505
    Roedy Green
    Mar 29, 2006
  2. Joost Ronkes Agerbeek

    auto_ptr: reassigning to member of owned object

    Joost Ronkes Agerbeek, Oct 2, 2003, in forum: C++
    Replies:
    2
    Views:
    474
    Joost Ronkes Agerbeek
    Oct 3, 2003
  3. NaeiKinDus
    Replies:
    1
    Views:
    558
    Jack Klein
    Apr 14, 2007
  4. Trans
    Replies:
    12
    Views:
    265
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    195
    David A. Black
    Jun 9, 2008
Loading...

Share This Page