虚函数的原理-虚函数表
单个类的虚函数表
#include <iostream>
using namespace std;
class Father {
public:
virtual void func1() { cout << "Father::func1" << endl; }
virtual void func2() { cout << "Father::func2" << endl; }
virtual void func3() { cout << "Father::func3" << endl; }
void func4() { cout << "非虚函数:Father::func4" << endl; }
public: //为了便于测试,特别该用public
int x = 100;
int y = 200;
static int z;
};
typedef void (*func_t)(void);
int Father::z = 1;
int main(void) {
Father father;
// 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
cout << "对象地址:" << (int*)&father << endl;
int* vptr = (int*)*(int*)&father;
cout << "虚函数表指针vptr:" << vptr << endl;
cout << "调用第1个虚函数: ";
((func_t) * (vptr + 0))();
cout << "调用第2个虚函数:";
((func_t) * (vptr + 1))();
cout << "调用第3个虚函数: ";
((func_t) * (vptr + 2))();
cout << "第1个数据成员的地址: " << endl;
cout << &father.x << endl;
cout << std::hex << (int)&father + 4 << endl;
cout << "第1个数据成员的值:" << endl;
cout << std::dec << father.x << endl;
cout << *(int*)((int)&father + 4) << endl;
cout << "第2个数据成员的地址: " << endl;
cout << &father.y << endl;
cout << std::hex << (int)&father + 8 << endl;
cout << "第2个数据成员的值:" << endl;
cout << std::dec << father.y << endl;
cout << *(int*)((int)&father + 8) << endl;
cout << "sizeof(father)==" << sizeof(father) << endl;
Father father2;
cout << "father的虚函数表:";
cout << *(int*)(*(int*)&father) << endl;
cout << "father2的虚函数表:";
cout << *(int*)(*(int*)&father2) << endl;
system("pause");
return 0;
}
执行效果:
内存分布:
对象内,首先存储的是“虚函数表指针”,又称“虚表指针”。
然后再存储非静态数据成员。
对象的非虚函数,保存在类的代码中!
对象的内存,只存储虚函数表和数据成员
(类的静态数据成员,保存在数据区中,和对象是分开存储的)
添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目
多个对象,共享同一个虚函数表!
使用继承的虚函数表
#include <iostream>
using namespace std;
class Father {
public:
virtual void func1() { cout << "Father::func1" << endl; }
virtual void func2() { cout << "Father::func2" << endl; }
virtual void func3() { cout << "Father::func3" << endl; }
void func4() { cout << "非虚函数:Father::func4" << endl; }
public: //为了便于测试,特别该用public
int x = 100;
int y = 200;
};
class Son : public Father {
public:
void func1() { cout << "Son::func1" << endl; }
virtual void func5() { cout << "Son::func5" << endl; }
};
typedef void (*func_t)(void);
int main(void) {
Father father;
Son son;
// 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
cout << "son对象地址:" << (int*)&son << endl;
int* vptr = (int*)*(int*)&son;
cout << "虚函数表指针vptr:" << vptr << endl;
for (int i = 0; i < 4; i++) {
cout << "调用第" << i + 1 << "个虚函数:";
((func_t) * (vptr + i))();
}
for (int i = 0; i < 2; i++) {
// +4 是因为先存储了虚表指针
cout << *(int*)((int)&son + 4 + i * 4) << endl;
}
system("pause");
return 0;
}
执行效果:
内存分布:
多重继承的虚函数表
#include <iostream>
using namespace std;
class Father {
public:
virtual void func1() { cout << "Father::func1" << endl; }
virtual void func2() { cout << "Father::func2" << endl; }
virtual void func3() { cout << "Father::func3" << endl; }
void func4() { cout << "非虚函数:Father::func4" << endl; }
public:
int x = 200;
int y = 300;
static int z;
};
class Mother {
public:
virtual void handle1() { cout << "Mother::handle1" << endl; }
virtual void handle2() { cout << "Mother::handle2" << endl; }
virtual void handle3() { cout << "Mother::handle3" << endl; }
public: //为了便于测试,使用public权限
int m = 400;
int n = 500;
};
class Son : public Father, public Mother {
public:
void func1() { cout << "Son::func1" << endl; }
virtual void handle1() { cout << "Son::handle1" << endl; }
virtual void func5() { cout << "Son::func5" << endl; }
};
int Father::z = 0;
typedef void(*func_t)(void);
int main(void) {
Son son;
int* vptr = (int*) * (int*)&son;
cout << "第一个虚函数表指针:" << vptr << endl;
for (int i = 0; i < 4; i++) {
cout << "调用第" << i + 1 << "个虚函数:";
((func_t) * (vptr + i))();
}
for (int i = 0; i < 2; i++) {
cout << *(int*)((int)&son + 4 + i * 4) << endl;
}
int* vptr2 = (int*) * ((int*)&son + 3);
for (int i = 0; i < 3; i++) {
cout << "调用第" << i + 1 << "个虚函数:";
((func_t) * (vptr2 + i))();
}
for (int i = 0; i < 2; i++) {
cout << *(int*)((int)&son + 16 + i * 4) << endl;
}
system("pause");
return 0;
}
执行结果
内存分布:
final
用来修饰类,让该类不能被继承
理解:使得该类终结!
class XiaoMi {
public:
XiaoMi(){}
};
class XiaoMi2 final : public XiaoMi {
XiaoMi2(){}
};
class XiaoMi3 : public XiaoMi2 { //不能把XiaoMi2作为基类
};
用来修饰类的虚函数,使得该虚函数在子类中,不能被重写
理解:使得该功能终结!
class XiaoMi {
public:
virtual void func() final;
};
void XiaoMi::func() { //不需要再写final
cout << "XiaoMi::func" << endl;
}
class XiaoMi2 : public XiaoMi {
public:
void func() {}; // 错误!不能重写func函数
};
override
override仅能用于修饰虚函数。
作用:
1.提示程序的阅读者,这个函数是重写父类的功能。
2.防止程序员在重写父类的函数时,把函数名写错。
#include <iostream>
using namespace std;
class XiaoMi {
public:
virtual void func() { cout << "XiaoMi::func" << endl; };
};
class XiaoMi2 : public XiaoMi {
public:
void func() override {}
//void func() override; 告诉程序员func是重写父类的虚函数
//void func1() override{} 错误!因为父类没有func1这个虚函数
};
int main(void) {
XiaoMi2 xiaomi;
return 0;
}
override只需在函数声明中使用,不需要在函数的实现中使用。
遗失的子类析构函数
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
class Father {
public:
Father(const char* addr ="中国"){
cout << "执行了Father的构造函数" << endl;
int len = strlen(addr) + 1;
this->addr = new char[len];
strcpy_s(this->addr, len, addr);
}
// 把Father类的析构函数定义为virtual函数时,
// 如果对 Father类的指针使用delete操作时,
// 就会对该指针使用“动态析构”:
// 如果这个指针,指向的是子类对象,
// 那么会先调用该子类的析构函数,再调用自己类的析构函数
virtual ~Father(){
cout << "执行了Father的析构函数" << endl;
if (addr) {
delete addr;
addr = NULL;
}
}
private:
char* addr;
};
class Son :public Father {
public:
Son(const char *game="吃鸡", const char *addr="中国")
:Father(addr){
cout << "执行了Son的构造函数" << endl;
int len = strlen(game) + 1;
this->game = new char[len];
strcpy_s(this->game, len, game);
}
~Son(){
cout << "执行了Son的析构函数" << endl;
if (game) {
delete game;
game = NULL;
}
}
private:
char* game;
};
int main(void) {
cout << "----- case 1 -----" << endl;
Father* father = new Father();
delete father;
cout << "----- case 2 -----" << endl;
Son* son = new Son();
delete son;
cout << "----- case 3 -----" << endl;
father = new Son();
delete father;
system("pause");
return 0;
}
【注意】
为了防止内存泄露,最好是在基类析构函数上添加virtual关键字,使基类析构函数为虚函数
目的在于,当使用delete释放基类指针时,会实现动态的析构:
如果基类指针指向的是基类对象,那么只调用基类的析构函数
如果基类指针指向的是子类对象,那么先调用子类的析构函数,再调用父类的析构函数
纯虚函数与抽象类
什么时候使用纯虚函数
某些类,在现实角度和项目实现角度,都不需要实例化(不需要创建它的对象),这个类中定义的某些成员函数,只是为了提供一个形式上的接口,准备让子类来做具体的实现。此时,这个方法,就可以定义为“纯虚函数”, 包含纯虚函数的类,就称为抽象类。
/*
纯虚函数的使用方法
用法:纯虚函数,使用virtual和 =0
*/
#include <iostream>
#include <string>
using namespace std;
class Shape {
public:
Shape(const string& color = "white") { this->color = color; }
virtual float area() = 0; //不用做具体的实现
string getColor() { return color; }
private:
string color;
};
class Circle : public Shape {
public:
Circle(float radius = 0, const string& color="White")
:Shape(color), r(radius){}
float area();
private:
float r; //半径
};
float Circle::area() {
return 3.14 * r * r;
}
int main() {
//使用抽象类创建对象非法!
//Shape s;
Circle c1(10);
cout << c1.area() << endl;
Shape* p = &c1;
cout << p->area() << endl;
system("pause");
return 0;
}
纯虚函数的注意事项:
父类声明某纯虚函数后,
那么它的子类,
1)要么实现这个纯虚函数 (最常见)
2)要么继续把这个纯虚函数声明为纯虚函数,这个子类也成为抽象类
3)要么不对这个纯虚函数做任何处理,等效于上一种情况(该方式不推荐)