beautypg.com

Multiple inheritance – HP Integrity NonStop J-Series User Manual

Page 281

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Multiple Inheritance

In

Chapter 15

, we built a Bus class by inheriting from

RWCollectable

. If we had an existing Bus

class at hand, we might have saved ourselves some work by using multiple inheritance to create
a new class with the functionality of both Bus and RWCollectable as follows:

class CollectableBus : public RWCollectable, public Bus {
.
.
.
};

This is the approach taken by many of the Rogue Wave collectable classes; for example, class

RWCollectableString

inherits from both class

RWCollectable

and class

RWCString

. The general

idea is to create your object first, then tack on the RWCollectable class to make the whole thing
collectable. This way, you will be able to use your objects for other things or in other situations,
where you might not want to inherit from class RWCollectable.

There is another good reason for using this approach: to avoid ambiguous base classes. Here's an
example:

class A { };

class B : public A { };

class C : public A { };

class D : public B, public C { };

void fun(A&);

main () {
D d;
fun(d); // Which A ?
}

There are two approaches to disambiguating the call to fun(). We can either change it to:

fun((B)d); // We mean B's occurrence of A

This manual is related to the following products: