Multiple inheritance in C
Although many books are recommended in software design rather than inheritance, inheritance still has many natural advantages, automatic ownership of base class members, without having to turn to call to call the required multiplexed To add more code.
Multi-inheritance In some cases, we can make our design more flexibility. Here we discuss some problems and solutions in multiple inheritance.
We realize an abstract base class A, then there have been many implementations, such as A1, A2, A3, at the beginning of the project, these A specific classes are very good, our software modules are also dependent on this An abstract base class A. all is well. As the project is carried out. We have entered another module development. Maybe the first consideration is no circumstances, and maybe the designer has other things when designing, here we have to use these A1, A2, A3. But we also found that these interface methods of abstract base category A have not met the functional requirements of this module. In this new module, we need some common methods to dry other things. What to do? We have to rewrite the A1, A2, A3, and join these general methods needed in the new module? But according to the interface-dependent principle developed, our software module can depend on abstract base class a? • These new generic methods are not declared in A. Maybe we should consider multiple inheritance, abstract the new generic method to a new interface B. This way we use the new method of using A1, just rely on this new interface B, and when using the A1 approach, it is only necessary to rely on interface A.
It's a good idea. So the new derived class is implemented. The A11 inherits the A1 and B, A22 inherits in A2 and B, and A33 inherits in the A3 and B, the application environment is as follows:
Bool app :: addsub (b * b)
{
A * a = dynamic_cast (b); // Dynamically converted to a;
IF (a! = 0)
A-> Methoda ();
Else
Return false // Conversion error, the input parameter is not the type we expect;
B-> Methodb (); // Call the method in interface B;
...
Return True;
}
It can be seen that the software module depends on an interface B, but also verifies whether it is inherited from A, otherwise it is not the type required for the module.
This module can run well. Note that the conversion of the type A is implemented by dynamic type conversion. And its specific implementation depends on RTTI, and in some cases, we cannot apply the RTTI mechanism. If we turn off the compiler's RTTI option. The above code may not run correctly. What should I do? If we know the mechanism of multiple inheritance, this may help us solve this problem. You can refer to C programming idea or depth to explore the relevant chapters of the C object model.
Here is a solution.
We add a polymorphic Virtual Void * getTHIS () method in B to return this pointer and have its implementation in A11, A22, and A33, in the application environment:
Bool app :: addsub (b * b)
{
A * a = static_cast (b-> getthis ());
A-> Methoda (); // Call the method in interface A;
B-> Methodb (); // Call the method in interface B;
...
Return True;
}