1 单继承的多态,以以下代码为例
class A
{
public:
virtual void f1()
{
cout<<"A::f1"<<endl;
}
virtual void f2()
{
cout<<"A::f2"<<endl;
}
void f3()
{
cout<<"A::f3"<<endl;
}
};
class B :public A
{
public:
virtual void f1()
{
cout<<"B::f1"<<endl;
}
virtual void f2()
{
cout<<"B::f2"<<endl;
}
void f3()
{
cout<<"A::f3"<<endl;
}
};
int main()
{
A* a;
B b;
a=&b;
a->f1();
a->f2();
a->f3();
return 0;
}
2.继承的多态以以下代码为例
#include<iostream>
using namespace std;
class A
{
public:
virtual void f1()
{
cout<<"A::f1"<<endl;
}
public:
int _a;
};
class B
{
public:
virtual void f2()
{
cout<<"B::f2"<<endl;
}
public:
int _b;
};
class C:public B,public A
{
public:
virtual void f3()
{
cout<<"C::f3"<<endl;
}
public:
int _c;
};
typedef void(*VFUNC)();
void PrintVTable(void* vtable)
{
printf("vtable:0x%p\n", vtable);
VFUNC* array = (VFUNC*)vtable;
for (size_t i = 0; array[i] != 0; ++i)
{
printf("vtable[%d]:0x%p->", i,array[i]);
array[i]();
}
cout<<"================================"<<endl;
}
int main()
{
A a;
B b;
C c;
PrintVTable(*((int**)&c));
return 0;
}
这个多继承说明了基类有虚表时,派生类会继承基类的虚数表,并且是先继承谁,就会把自己的虚函数存储在谁的虚表中,我们通过调用监视窗口和打印虚表来看清这一问题。
所以说C有两个虚表。
3.菱形继承
class A
{
public:
virtual void f1()
{
cout<<"A::f1"<<endl;
}
public:
int _a;
};
class B:public A
{
public:
virtual void f1()
{
cout<<"B::f1"<<endl;
}
virtual void f2()
{
cout<<"B::f2"<<endl;
}
public:
int _b;
};
class C:public A
{
public:
virtual void f3()
{
cout<<"C::f3"<<endl;
}
public:
int _c;
};
class D:public B,public C
{
public:
virtual void f1()
{
cout<<"D::f1"<<endl;
}
virtual void f4()
{
cout<<"D::f4"<<endl;
}
public:
int _d;
};
typedef void (*V_FUNC)();
void PrintVTable(int vtable)
{
int* VfArray = (int*)vtable;
printf("vtable:0x%p\n",VfArray);
for(int i = 0;VfArray[i] != 0;++i)
{
printf("vfun[%d]:0x%p->",i,VfArray[i]);
V_FUNC f = (V_FUNC)VfArray[i];
f();
}
cout<<"*********************************"<<endl;
}
int main()
{
A a;
B b;
C c;
D d;
d.B::_a=1;
d.C::_a=2;
d._b=3;
d._d=4;
PrintVTable(*((int*)&d));
PrintVTable(*(int*)((char*)&d+12));
return 0;
}
菱形继承也符合单继承和多继承的规律
总结:
当基类有虚函数即有虚表时,派生类就会继承其虚表。一个只有虚表的类就是4个字节大小,就是一个指针的大小。