Inheritance query

M

Mahesh

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
 
H

Howard

Mahesh said:
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
 
V

Victor Bazarov

Mahesh said:
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
 
V

Victor Bazarov

Howard said:
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.
 
M

Mahesh

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.
 
H

Howard

Victor Bazarov said:
Howard said:
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
 
P

Prog37

Mahesh said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top