In the C ++ multi-inheritance system, you can override virtual functions in different base classes in the derived classes. The following is an example:
[Cpp]
Class CBaseA
{
Public:
Virtual void TestA ();
};
Class CBaseB
{
Public:
Virtual void TestB ();
};
Class CDerived: public CBaseA, public CBaseB
{
Public:
Virtual void TestA (); // rewrite the virtual function TestA () in the base class CBaseA ()
Virtual void TestB (); // rewrite the virtual function TestB () in the base class CBaseB ()
};
Void Test ()
{
CDerived D;
CBaseA * pA = & D;
CBaseB * pB = & D;
PA-> TestA (); // call the TestA () function of the CDerived class
PB-> TestB (); // call the TestB () function of the CDerived class
}
However, if two base classes have a virtual function with the same prototype, for example:
[Cpp]
Class CBaseA
{
Public:
Virtual void Test ();
};
Class CBaseB
{
Public:
Virtual void Test ();
};
How do I rewrite these two virtual functions with the same prototype in a derived class? This may not be a common situation, but it does exist. For example, the two class libraries used during development are provided by different vendors, or these two class libraries are developed by different development teams of the company. For the former, it is impossible to modify the interface of the base class; for the latter, it is costly to modify the interface. If you directly override this virtual function in a derived class, the Test () virtual function of the two base classes will be overwritten. In this way, there is only one Test () implementation, rather than different implementations as in the previous example.
[Cpp]
Class CDerived: public CBaseA, public CBaseB
{
Public:
Virtual void Test ();
};
Void Test ()
{
CDerived D;
CBaseA * pA = & D;
CBaseB * pB = & D;
// The following two lines of code call the Test () function of the CDerived class.
PA-> Test ();
PB-> Test ();
}
To implement the same way in the first example, In the derived class CDerived, rewrite the virtual function Test () with the same prototype as the base class, you can use the following method. First, there is no need to modify the two base classes (in actual development, the possibility of modifying the base classes is very small ).
[Cpp]
Class CBaseA
{
Public:
Virtual void Test ();
};
Class CBaseB
{
Public:
Virtual void Test ();
};
Now, add two intermediate classes for this inheritance system, which are derived from two base classes respectively.
[Cpp]
Class CMiddleBaseA: public CBaseA
{
Private:
// Real implementation functions
// Set it to a pure virtual function, which must be implemented in the derived class
Virtual void CBaseA_Test () = 0;
// Rewrite the inherited virtual function
// Directly call the real implementation function
Virtual void Test ()
{
CBaseA_Test ();
}
};
// Use the same method as CMiddleBaseA
Class CMiddleBaseB: public CBaseB
{
Private:
Virtual void CBaseB_Test () = 0;
Virtual void Test ()
{
CBaseB_Test ();
}
};
Then, two intermediate classes above the class CDerived are derived as the base class. Rewrite different pure virtual functions in the above two base classes and add different implementation codes.
[Cpp]
Class CDerived: public CMiddleBaseA, public CMiddleBaseB
{
Private:
// Rewrite the virtual functions inherited from the intermediate class
Virtual void CBaseA_Test (); // here the Test () of CBaseA is actually rewritten ()
Virtual void CBaseB_Test (); // here the Test () of CBaseB is actually rewritten ()
};
Void Test ()
{
CDerived D;
CBaseA * pA = & D;
CBaseB * pB = & D;
// Call the Test () function of CBaseA.
// Due to the C ++ polymorphism, The CBaseA_Test () function in the CDervied class is actually called.
PA-> Test ();
// Call the Test () function www.2cto.com of the CBaseB class.
// Due to the C ++ polymorphism, The CBaseB_Test () function in the CDervied class is actually called.
PB-> Test ();
}
In the above Code, pA-> Test (); this line of code shows how the above solution is implemented. First, because the virtual function Test () is overwritten in the CMiddleBaseA derived class CBaseA, this line of code calls the Test () function of the class CMiddleBaseA. Then, Test () of the class CMiddleBaseA () the function calls the implementation function CBaseA_Test (). Finally, because the virtual function CBaseA_Test () is overwritten in the CDerived derived class of CMiddleBaseA, The CBaseA_Test () in the CDerived class is actually called () function. Similarly, the Code pB-> Test (); actually calls the CBaseB_Test () function in the CDervied class. Through the above method, you can re-write virtual functions with the same prototype in different base classes in C ++ multi-inheritance.
From happy