多态就是一个事物再不同场景时的多种形态,编程语言中就是完成某个行为时,当不同对象去完成会产生不同的效果。
多态的定义及实现
- 调用函数的对象必须为指针或引用
- 被调用函数必须是虚函数,且完成了虚函数的重写
虚函数定义:
定义类的成员函数时,函数前加virtual关键字。
虚函数的重写:
派生类中存在和基类完全相同(函数名,参数,返回值)的虚函数,称为子类重写了父类的虚函数。
特例:协变 重写的虚函数的返回值可以不同,但必须为基类或派生类的引用/指针。
#include<iostream>
using namespace std;
//协变
class A{
public:
virtual A* fun()
{
cout<<"A::fun()"<<endl;
return this;
}
};
class B : public A{
public:
virtual B* fun()
{
cout<<"B::fun()"<<endl;
return this;
}
};
int main()
{
A a;
a.fun();
B b;
b.fun();
}
重写虚函数的规范行为:重写的函数,基类和派生类都加上关键字virtual.
析构函数的重写:
析构函数最好写成虚函数。避免父类指针指向子类对象时,析构时调用父类的析构函数(不构成多态)。
析构函数是一个特殊的函数,在编译时,编译器统一将析构函数名称处理为destructor。
//虚函数的重写
#include<iostream>
using namespace std;
class A{
public:
~A()
{
cout<<"~A()"<<endl;
}
};
class B : public A{
public:
~B()
{
cout<<"~B()"<<endl;
}
};
int main()
{
A* a = new B;
delete a;
}
重写,重载,重定义对比:
抽象类
包含纯虚函数的类称为抽象类。
纯虚函数的定义: 虚函数后加 =0即为纯虚函数。
抽象类无法实例化。
C++11中的override,final:
final修饰的基类虚函数不能被重写。
override修饰派生类的虚函数,强制完成重写。
多态的实现原理
#include<iostream>
using namespace std;
class A{
public:
virtual A* fun()
{
cout << "A::fun()" << endl;
return this;
}
virtual ~A()
{
cout << "~A()" << endl;
}
int _a;
};
class B : public A{
public:
virtual B* fun()
{
cout << "B::fun()" << endl;
return this;
}
virtual ~B()
{
cout << "~B()" << endl;
}
int _b;
};
int main()
{
A a;
B b;
system("pause");
return 0;
}
由图中可见,a中不仅含有成员变量_a还有一个 _vfptr指针,_vfptr(virtual function pointer)指针称为虚函数表指针,指向虚函数表,虚函数表中存放着对应类的虚函数指针。
观察B类型的b中的虚函数,可见B中的fun对A中的fun完成了重写。B的虚函数表中的fun代表的是
B::fun(),而在a中的fun为A::fun()。
多态的实现依靠的是派生类对基类中的虚函数的重写,所实现的。
所以对类中虚函数的调用只与 引用或指针 指向的对象本身有关。由此解释了析构函数需要写成虚函数的原因,图中也可看出析构函数的函数名称相同。
虚函数指针存在对象的头部为对象的第一个成员。
多继承中的虚函数表
单继承中的虚函数表为单一虚函数表,这里就不赘述。派生类中定义的虚函数在虚函数表的后添加。
//单继承
#include<iostream>
using namespace std;
typedef void(*VFPTR)(); //定义函数指针
class Base{
public:
virtual void fun1(){cout<<"This is Base::fun1"<<endl;};
virtual void fun2(){cout<<"This is Base::fun2"<<endl;};
private:
int a;
};
class Drive: public Base{
public:
virtual void fun1(){cout<<"This is Drive::fun1"<<endl;};
virtual void fun3(){cout<<"This is Drive::fun3"<<endl;};
virtual void fun4(){cout<<"This is Drive::fun4"<<endl;};
private:
int b;
};
void Print(VFPTR vTable[]) //打印虚函数表
{
cout<<"虚表地址"<<vTable<<endl;
for(int i=0; vTable[i] != nullptr; i++)
{
printf("第%d个虚函数地址:0X%x ->",i,vTable[i]);
VFPTR f = vTable[i];
f();
}
cout<<endl;
}
int main()
{
Base b;
Drive d;
VFPTR* vTableb = (VFPTR*)(*(int *)(&b)); //虚函数表存在对象的头部
Print(vTableb);
VFPTR* vTabled = (VFPTR*)(*(int *)(&d));
Print(vTabled);
return 0;
}
多继承中的虚函数表
vs中的运行结果
gcc中和此不同
#include<iostream>
using namespace std;
typedef void(*VFPTR)();
class Base_first{
public:
virtual void fun1(){ cout << "This is Base_first::fun1" << endl; }
virtual void fun2(){ cout << "This is Base_first::fun2" << endl; }
private:
int _first;
};
class Base_second{
public:
virtual void fun1(){ cout << "This is Base_second::fun3" << endl; }
virtual void fun2(){ cout << "This is Base_second::fun4" << endl; }
private:
int _second;
};
class Drive : public Base_first, public Base_second{
public:
virtual void fun1(){ cout << "This is Drive::fun1" << endl; }
virtual void fun3(){ cout << "This is Drive::fun3" << endl; }
};
void Print(VFPTR vTable[])
{
cout << "虚表地址" << vTable << endl;
for (int i = 0; vTable[i] != nullptr; i++)
{
printf("第%d个虚函数地址:0X%x ->", i, vTable[i]);
VFPTR f = vTable[i];
f();
}
cout << endl;
}
int main()
{
Drive d;
VFPTR* vTabled1 = (VFPTR*)(*(int *)(&d));
Print(vTabled1);
VFPTR* vTable2 = (VFPTR*)(*(int*)((char*)&d + sizeof(Base_first)));
Print(vTable2);
return 0;
}
- 多继承中派生类未重写的虚函数放在第一个直接父类的虚函数表中