"Mahesh" <sr*************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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