Question about singleton

Discussion in 'C++' started by Jack, Oct 18, 2007.

  1. Jack

    Jack Guest

    If class A is a singleton, class B inherits class A,

    class B : public class A

    Is class B also a singleton?

    Thanks.

    Jack
    Jack, Oct 18, 2007
    #1
    1. Advertising

  2. Jack

    Kai-Uwe Bux Guest

    Jack wrote:

    > If class A is a singleton, class B inherits class A,
    >
    > class B : public class A
    >
    > Is class B also a singleton?


    Well, if class A is a singleton, it will ensure that only one instance is
    created. Since creation of a B instance requires creation of an A instance,
    you can only create one. In that sense, class B is a singleton.

    However, if I recall correctly, there was a little more to a singleton:
    something about a uniform point of access. It is not clear at all, whether
    and how class B could inherit that part. In order to support that, class A
    would need to be designed appropriately.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 18, 2007
    #2
    1. Advertising

  3. On 2007-10-18 08:48, Jack wrote:
    > If class A is a singleton, class B inherits class A,
    >
    > class B : public class A
    >
    > Is class B also a singleton?


    Usually a singleton have a private constructor, which means that you can
    not create an A instance when creating the B instance, in other words
    you can not inherit from a singleton.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 18, 2007
    #3
  4. Jack

    Uday Bidkar Guest

    On Oct 18, 11:48 am, Jack <> wrote:
    > If class A is a singleton, class B inherits class A,
    >
    > class B : public class A
    >
    > Is class B also a singleton?
    >
    > Thanks.
    >
    > Jack


    It depends on how singleton pattern is implemented in class A. I guess
    if the constructor of A is made private, then you wont be able to
    initialize even a single object of B and if its protected, you can
    implement B as singleton and non-singleton as well as you would be
    able to call A's constructo from that of B.
    Uday Bidkar, Oct 18, 2007
    #4
  5. Jack

    Jack Guest

    On Oct 18, 1:16 am, Uday Bidkar <> wrote:
    > On Oct 18, 11:48 am, Jack <> wrote:
    >
    > > If class A is a singleton, class B inherits class A,

    >
    > > class B : public class A

    >
    > > Is class B also a singleton?

    >
    > > Thanks.

    >
    > > Jack

    >
    > It depends on how singleton pattern is implemented in class A. I guess
    > if the constructor of A is made private, then you wont be able to
    > initialize even a single object of B and if its protected, you can
    > implement B as singleton and non-singleton as well as you would be
    > able to call A's constructo from that of B.


    How about this implementation:

    class A{

    public:
    virtual A* getinstance()
    {
    if(ptr_A == NULL)
    ptr_A = new A;
    return ptr_A;
    }

    protected:
    A():ptr_A(NULL){}

    private:
    A* ptr_A;
    };

    class B : public class A
    {
    public:
    A* getinstance()
    {
    if(ptr_B == NULL)
    ptr_B = new B;
    return ptr_B;
    }

    protected:
    B(): ptr_B(NULL),A(NULL){}

    private:
    B* ptr_B;
    };

    Thanks.

    Jack
    Jack, Oct 18, 2007
    #5
  6. Jack

    Uday Bidkar Guest

    Could you please elaborate on what exactly you want to figure out in
    post above?
    Uday Bidkar, Oct 19, 2007
    #6
  7. Uday Bidkar wrote:
    > On Oct 18, 11:48 am, Jack <> wrote:
    >> If class A is a singleton, class B inherits class A,


    You're probably better off if you can refactor the class so that the
    singleton nature can be handled with dependency injection, rather than
    making it an explicit singleton.

    Peace,
    Gary
    --
    --------------------------------------------------------------------
    Gary Coulbourne Software Developer
    C/C++, Java, Perl, Python
    Gary Coulbourne, Oct 22, 2007
    #7
    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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,214
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    153
  3. Trans
    Replies:
    12
    Views:
    265
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    195
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    195
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page