Friday, August 15, 2014

Multiple Inheritance in C++

Introduction:

One of the most important SOLID principles is the Open-Closed principle which specifies that software entities (classes, modules, functions, etc...) should be open for extension, but closed for modification. As we learned in class, many of the software design paradigms make heavy use of extension as a way to make the code base more readable and less repetitive. The paper I read concerns itself with Multiple Inheritance In C++. Multiple Inheritance has always been a tricky subject for many developers. So much so that many languages don't even support it. For example, Java only allows for inheritance from a single parent class but allows you to implement multiple interfaces to make up for the lack of the feature. The concern with multiple inheritance occurs when a class D extends Class B and Class C, but Class B and Class C in turn have both inherited and perhaps overridden methods from Class A. The question arrises, which methods does Class D inherit? Methods that may have been inherited from Class A by B or C? or perhaps is it better to go straight to A? The short answer is that it inherits all of them. This problem is usually referred to as the deadly diamond where Class A and D represent the top and bottom corners respectfully of the inheritance hierarchy and the Class B and C make up the sides. Lets see what the creator and designer of C++, Bjarne Stroustrup has to say about Multiple Inheritance in C++ in this paper.

Salient Points:

Here is a short example of multiple inheritance can be achieved through C++. By no means is this example complete and you should refer to the paper for a better idea of how this is achieved.

Given two classes:
class A{...}
class B{...}
//a user can define a third class 
class C : A, B{...}

Calling a member function of A or C is identical to what is done in the single inheritance case. But if you are given a C* and want to access the inherited member function of B you have to be a little more explicit:

C* pc;
pc->bf(2)
//Assume that bf is a member of B and C has no other member named bf except the inherited one of B

In this case the compiler is able to figure out what it needs to do. The second line is converted and the pointer is advanced to the location in memory where bf is located. But what happens if both A and B have a public member ii:

class A { int ii; };
class B { char* ii; };
class C : A, B { };

Now class C has two members called ii, A::ii and B::ii. Then

C* pc;
pc->ii; // error: A::ii or B::ii ?

This will be illegal as there is ambiguity over which member is sought after. We need to be a bit more explicit:

pc->A::ii; // C’s A’s ii
pc->B::ii; // C’s B’s ii

What happens when we are not trying to access a member but instead a function f()?

class A { void f(); };
class B { int f(); };
class C : A, B { };
C* pc;
pc->f(); // error: A::f or B::f ?
pc->A::f(); // C’s A’s f
pc->B::f(); // C’s B’s f

This can however become cumbersome quickly and so as an alternative to explicitly identifying the base class every time, you can define an f() in C that calls the base functions as required.

class C : A, B {
int f() { A::f(); return B::f(); }
};
C* pc;
pc->f(); // C::f is called

As Stroustrup claims: "This solution usually leads to cleaner programs; it localizes the specification of the meaning of the name for objects of a derived class to the declaration of the derived class."

Finally addressing the problem of the deadly diamond. if class A and B inherit from another class say L, you can specify further up the chain which inherited method you want to use:

class L { ... };
class A : L { ... };
class B : L { ... };
class C : A , B { ... };

Assume that class L in the example above has a member m. Then a function C::f could refer to L::m by explicit qualification:

void C::f() { A::m = B::m; }

You could also be more explicit:

void C::f() { A::L::m = B::L::m; }

And so by being explicit you can specify exactly which method you are trying to call.

Conclusion:

First off, please be sure to read through the paper in its entirety as it outlines how to handle virtual functions which unfortunately we did not cover in our class this summer. The paper goes into good detail about how things can change when using virtual functions. Also, the paper was very helpful at understanding how memory is managed and where pointers are pointing to when dealing with multiple inheritance.

Multiple inheritance is definitely useful in many instances. Although C++ is a pretty complicated language and things are already tricky, it is important to be able to know how to use extension and inheritance. The trick to multiple inheritance in this case is to simply be more explicit about the members and functions you call. If you are going to extend a class its probably a good idea to be in the habit of being explicit in your method calls or to simply override any method you plan to use and be explicit within its definition. I'll give a final word on performance when it comes to multiple inheritance. Since much of the hard work is done at compile time it is important to note that performance is not hindered much in the final program. More importantly you should not be afraid to use multiple inheritance if you are worried about errors. The complier will force you to be explicit as it is so there shouldn't be any fear of messing up. With that said, C++ has its intricacies and so you should write plenty of good test cases and asserts to make sure your implementation is correct.

No comments:

Post a Comment