An example of C++ multiple inheritance polymorphism

  • 2020-05-26 09:39:00
  • OfStack

The implementation of C++ multiinherited polymorphism

If a virtual function exists in a class, the compiler generates a virtual function pointer to the object of the class when it declares the object of the class. The virtual function pointer points to the corresponding virtual function table of the class.

Polymorphism is realized because of the use of a dynamic binding mechanism, which does not determine the address of the calling function during compilation, and queries the virtual function table pointed to by the virtual function pointer when the virtual function is called.

The virtual function pointer in the object generated by the derived class points to the virtual function table of the derived class, so whether the base class or the derived class is called, the table of the derived class is queried and the function of the derived class is called.

What happens if multiple inheritance occurs and multiple base classes have virtual functions? How are virtual function Pointers arranged, and how can multiple base class Pointers point to derived class objects at the same time, while polymorphism occurs simultaneously?

Look at the program below


#include <stdio.h>
#include <iostream>
using namespace std;

class Base1{

  public:
  void fun()
  {
    printf("this is Base1 fun\n");
  }
  virtual void fun1()
  {
    printf("this is Base1 fun1\n");
  }
};

class Base2{
  public:
  void fun()
  {
    printf("this is Base2 fun\n");
  }
  virtual void fun2()
  {
    printf("this is Base2 fun1\n");
  }
};

class Derived : public Base1,public Base2{
  public:
  void fun()
  {
    printf("this is Derived fun\n");
  }
  void fun1()
  {
    printf("this is Derived fun1\n");
  }
  void fun2()
  {
    printf("this is Derived fun2\n");
  }
};

int main()
{
  Derived *pd = new Derived();
  Base1 *p1 = (Base1 *)pd;
  Base2 *p2 = (Base2 *)pd;
  p1->fun();
  p2->fun();
  p1->fun1();
  p2->fun2();
  printf("Base1 p1:%x\n", p1);
  printf("Base2 p2:%x\n", p2);
  return 0;
}

The results are as follows


feng@mint ~/code/c++/cpp_muti_drived 
$ ./muti_derived 
this is Base1 fun
this is Base2 fun
this is Derived fun1
this is Derived fun2
Base1 p1:2097c20
Base2 p2:2097c28

The Derived class inherits Base1 and Base2, respectively. According to the results, polymorphism occurs in both classes. A base class pointer calls a function, all of which are derived class objects.

By printing out the addresses of p1 and p2 and finding that they are 8 bytes apart, you can see that in the process of type conversion, if you pass the address to the pointer of the second base class, you will automatically subtract 8 from the address, which, in a 64-bit system, is exactly the length of a pointer. So p2 is actually pointing to the address of the second virtual function pointer, so you can implement multiinheritance polymorphism.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: