C ++ polymorphism
C ++ has been known in polymorphism (c), the virtual address of the function table stored in memory model object by the pointer (vfptr), access to the virtual function table of the object.
class Base
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
virtual void h(){ cout << "Base::h()" << endl; }
private:
int _b1 = 1;
int _b2 = 2;
};
Base class for the object b, b & b is the address in memory.
Virtual function table in the general inheritance
Under normal inheritance, base and derived class virtual function coverage may occur, there may be their own virtual function.
Inheritance general (non-virtual function override)
The following is a function without the cover of the inheritance, the inheritance hierarchy where no coverage.
class Base
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
virtual void h(){ cout << "Base::h()" << endl; }
private:
int _b1 = 1;
int _b2 = 2;
};
class Derived : public Base
{
public:
virtual void f1(){ cout << "Derived::f1()" << endl; }
virtual void g1(){ cout << "Derived::g1()" << endl; }
virtual void h1(){ cout << "Derived::h1()" << endl; }
};
Can be found, the derived class virtual function table in the base class virtual function table in front of the virtual function, followed by the derived class virtual function , and the virtual function table to 0 (run under VS2013) is the terminator flag.
General succession (cover has virtual functions)
class Base
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
virtual void h(){ cout << "Base::h()" << endl; }
private:
int _b1 = 1;
int _b2 = 2;
};
class Derived : public Base
{
public:
virtual void f(){ cout << "Derived::f()" << endl; } //存在覆盖的情况
virtual void g1(){ cout << "Derived::g1()" << endl; }
virtual void h1(){ cout << "Derived::h1()" << endl; }
};
Can be found, the derived class f () member function in the base class covers f () member function, the virtual function table we see the derived class, at least one address, the address may be a function of points found that the lack of what the derived class virtual base class virtual function function covered .
Virtual function tables in multiple inheritance
No cover multiple inheritance function
The derived class virtual function in a base class first, the rest of the virtual function table storing the respective virtual function itself
Function covering the presence of multiple inheritance
class Base1
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
virtual void h(){ cout << "Base::h()" << endl; }
};
class Base2
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
virtual void h(){ cout << "Base::h()" << endl; }
};
class Base3
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
virtual void h(){ cout << "Base::h()" << endl; }
};
class Derived : public Base1, public Base2, public Base3
{
public:
virtual void f(){ cout << "Derived::f()" << endl; } //发生覆盖情况
virtual void g1(){ cout << "Derived::g1()" << endl; }
virtual void h1(){ cout << "Derived::h1()" << endl; }
};
Multiple inheritance, the derived class object structure, as follows:
We can see the position of three parent class virtual function f of the table () function are replaced with sub-class virtual function.
Any static type parent pointer to the subclass will virtual function call subclass .
Derive d;
Base1 *b1 = &d;
Base2 *b2 = &d;
Base3 *b3 = &d;
b1->f(); //Derive::f()
b2->f(); //Derive::f()
b3->f(); //Derive::f()