Multiple inheritance in C ++
Although in software design, many books recommend that you use combinations instead of inheritance, inheritance still has many natural advantages and automatically owns the base class members, instead of turning to the reusable members of the call like a combination, more code is added.
In some cases, multi-inheritance can make our design more flexible. Next we will discuss some problems and solutions in Multi-inheritance.
We have implemented an abstract base class A, and thus derived many implementation classes, such as A1, A2, and A3. At the beginning of the project, these specific classes of a work well, our software module also relies on this abstract base class. Everything is fine. As the project progresses. We started the development of another module. Maybe we didn't have a good idea at the beginning. Maybe the designer has something else during design. here we need to use these A1, A2, and A3 again. However, we also found that these interface methods of abstract base class A cannot meet the functional requirements of this module. In this new module, we need another common method to do other things. What should I do? Do we need to rewrite A1, A2, A3, and add these general methods required in the new module? But according to the interface dependency principle of software development, can our software module still depend on abstract base class? However, these newly added general methods are not declared in. Maybe we should consider multiple inheritance and abstract the new general method into a new interface B. In this way, when using the new method of A1, we only need to rely on this new interface B, while when using methods earlier than A1, we only need to rely on interface.
That's a good idea. So the new derived class is implemented. A11 inherits from A1 and B, A22 inherits from A2 and B, and A33 inherits from A3 and B. The application environment is as follows:
Bool app: addsub (B * B)
{
A * A = dynamic_cast <A *> (B); // converts data to a dynamically;
If (! = 0)
A-> methoda ();
Else
Return false // Conversion error. The input parameter is not of the expected type;
B-> methodb (); // call the method in interface B;
...
Return true;
}
As you can see, the software module depends on an interface B and also verifies whether it inherits from A. Otherwise, it is not the type required by the module.
This module can run well. However, note that type A is converted dynamically. The specific implementation depends on rtti. In some cases, we cannot apply the rtti mechanism. If we disable the rtti option of the compiler. The above code may fail to run correctly. What should we do? If we know the mechanism of Multi-inheritance, this may help us solve this problem. You can refer to the C ++ programming idea or explore the C ++ Object Model in depth.
The following is a solution.
We add a multi-state virtual void * getthis () method in B to return the this pointer and implement it in A11, A22, and A33. In the application environment:
Bool app: addsub (B * B)
{
A * A = static_cast <A *> (B-> getthis ());
A-> methoda (); // call the method in interface;
B-> methodb (); // call the method in interface B;
...
Return true;
}
In the above implementation, the getthis () method of polymorphism is used to obtain the pointer of a specific implementation class and convert it to a. However, because the compiler disables rtti support, this conversion is not of type security, we need to specify in advance that the input type includes the implementation of a and B, otherwise the conversion will not achieve our expected results.