Inheritance query

Discussion in 'C++' started by Mahesh, Mar 11, 2005.

  1. Mahesh

    Mahesh Guest

    Hello,
    I am having a query about inheritance.
    I have a virtual base class that is derived by to other classes that
    are hirarchially at the same level. But I still need to ensure that
    only a single instance of the base class is created.
    Is this even possible? If yes then how do I go about doing this?
    Thanks,
    Mahesh
    Mahesh, Mar 11, 2005
    #1
    1. Advertising

  2. Mahesh

    Howard Guest

    "Mahesh" <> wrote in message
    news:...
    > Hello,
    > I am having a query about inheritance.
    > I have a virtual base class that is derived by to other classes that
    > are hirarchially at the same level. But I still need to ensure that
    > only a single instance of the base class is created.
    > Is this even possible? If yes then how do I go about doing this?
    > Thanks,
    > Mahesh
    >


    I'm not sure what you mean by a "virtual base class". How are your derived
    classes derived? Are you using virtual inheritance? Or, are you actually
    using public inheritance, and you really meant either just "base class", or
    perhaps "abstract base class"?

    If you're using public inheritance, then your derived class looks something
    like this:

    class MyDerivedClass : public MyBaseClass

    In that case, every instance of a derived class must by definition include
    everything that the base class defines. There is no base class instance to
    which derived class instances refer to. An instance of the derived class IS
    a base class object, PLUS anything additional that the derived class
    defines.

    In this case, if you want a single instance of some object to exist that all
    the other objects can refer to, then what you need is containment, not
    inheritance. For instance, you can declare a member of a common base class
    as a pointer to a singleton class that all objects (whether of the base or
    derived classes) can refer to.

    If, on the other hand, you're using virtual inheritance, then you have
    something like this:

    class MyDerivedClass : virtual MyBaseClass

    In that case, you're into territory I've never explored. Let others know if
    that's what you mean, and they (or a good book) can give you details on what
    happens in that case.

    -Howard
    Howard, Mar 11, 2005
    #2
    1. Advertising

  3. Mahesh wrote:
    > I am having a query about inheritance.
    > I have a virtual base class that is derived by to other classes that
    > are hirarchially at the same level. But I still need to ensure that
    > only a single instance of the base class is created.
    > Is this even possible? If yes then how do I go about doing this?


    You cannot ensure that. IOW, it's not possible. A derived class will
    have an instance of the base class in it. You cannot make derived
    class instances share an instance of any base class.

    That said, you can make objects share another object that would exist
    as the only instance of its type in the program. That mechanism is
    often called "a singleton" and you will need to share the singleton
    by using a pointer or a reference to it in the classes who need it:

    class MySingleton { }; // read about singletons and how to create them

    class MyBase {
    MySingleton *singleton;
    public:
    // whatever
    };

    class MyDerived_1 : public MyBase {
    ...
    };

    class MyDerived_2 : public MyBase {
    ...
    };

    Now, all the functionality and the state you need to be represented by
    the singleton should be implemented in 'MySingleton' class and proxied
    in 'MyDerived_X' classes by the 'MyBase' class, which itself does not
    have any data except a pointer to your MySingleton object.

    V
    Victor Bazarov, Mar 11, 2005
    #3
  4. Howard wrote:
    > "Mahesh" <> wrote in message
    > news:...
    >
    >>Hello,
    >>I am having a query about inheritance.
    >>I have a virtual base class that is derived by to other classes that
    >>are hirarchially at the same level. But I still need to ensure that
    >>only a single instance of the base class is created.
    >>Is this even possible? If yes then how do I go about doing this?
    >>Thanks,
    >>Mahesh
    >>

    >
    >
    > I'm not sure what you mean by a "virtual base class". How are your derived
    > classes derived? Are you using virtual inheritance? [...]


    The term "virtual base class" is used to describe the base class from
    which the derived classes inherit virtually, yes.

    When a class is defined, there is nothing that can be said about it except
    that it's abstract. It doesn't become "base" until some other class is
    derived from it. There are two ways to derive: normal and virtual. Both
    can be further complicated by access specifiers, but that doesn't change
    how many instances there are and how the class instance is[are] created
    and initialised in the derived class[es]' instances.

    > In that case, you're into territory I've never explored. [...]


    Then now is your chance. Read before you write.
    Victor Bazarov, Mar 11, 2005
    #4
  5. Mahesh

    Mahesh Guest

    Howard,
    I am using public inheritance. the base class is not virtual... I am
    mistaken. ALso like you said I want an instance of the base class that
    instances of the child classes can look at.
    I also want it such that when the child classes are instantiated, only
    one instance of the base class is created.
    Mahesh, Mar 11, 2005
    #5
  6. Mahesh

    Mahesh Guest

    Thanks guys....
    Mahesh, Mar 11, 2005
    #6
  7. Mahesh

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:yYkYd.52248$01.us.to.verio.net...
    > Howard wrote:
    >> "Mahesh" <> wrote in message
    >> news:...
    >>
    >>>Hello,
    >>>I am having a query about inheritance.
    >>>I have a virtual base class that is derived by to other classes that
    >>>are hirarchially at the same level. But I still need to ensure that
    >>>only a single instance of the base class is created.
    >>>Is this even possible? If yes then how do I go about doing this?
    >>>Thanks,
    >>>Mahesh
    >>>

    >>
    >>
    >> I'm not sure what you mean by a "virtual base class". How are your
    >> derived classes derived? Are you using virtual inheritance? [...]

    >
    > The term "virtual base class" is used to describe the base class from
    > which the derived classes inherit virtually, yes.
    >
    > When a class is defined, there is nothing that can be said about it except
    > that it's abstract. It doesn't become "base" until some other class is
    > derived from it. There are two ways to derive: normal and virtual. Both
    > can be further complicated by access specifiers, but that doesn't change
    > how many instances there are and how the class instance is[are] created
    > and initialised in the derived class[es]' instances.
    >
    >> In that case, you're into territory I've never explored. [...]

    >
    > Then now is your chance. Read before you write.


    Interesting, thanks. I guess I didn't remember that term because when I hit
    the part about multiple inheritance and virtual derivation, my eyes kind of
    glazed over and I didn't recover consciousness until I'd passed on to other
    subjects. :)

    Thanks for the info, and sorry for the sidetrack...

    -Howard
    Howard, Mar 11, 2005
    #7
  8. Mahesh

    Prog37 Guest

    Mahesh wrote:
    > Howard,
    > I am using public inheritance. the base class is not virtual... I am
    > mistaken. ALso like you said I want an instance of the base class that
    > instances of the child classes can look at.
    > I also want it such that when the child classes are instantiated, only
    > one instance of the base class is created.
    >

    It sounds like you want to have a singleton of the base class
    but allowing one and only instance from a set of derived classes.
    Is that correct? Or are you asking for multiple instances of the derived
    class to share a single instance of the base class.

    The former is usually called the singleton pattern. The latter is not
    even polymorphic and the derived classes should use the has-a pattern.

    Assuming its the first case which is more common and makes more sense,
    one way to accomplish your goal is to override the new operator in the
    base class. The overrided new operator would maintain a counter that is
    initialized to zero, it would only allocate memory when the counter is
    zero. On every call after the first it would keep return NULL. This
    would guarantee that only the first instance could be created. All
    attempts after the first would fail.
    Prog37, Mar 12, 2005
    #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. maxw_cc
    Replies:
    1
    Views:
    3,120
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    806
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,026
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,877
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    762
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page