虚函数表
虚函数表是一块连续的内存,每个内存单元中记录一个JMP指令的地址.
类中虚函数的存储借助虚函数表,请看代码.
#include <iostream>
using namespace std;
class Father {
public:
virtual void function1() { cout << "调用了Father的方法1" << endl; }
virtual void function2() { cout << "调用了Father的方法2" << endl; }
private:
int x = 0;
};
int main(void) {
Father father;
system("pause");
return 0;
}
下图很好地展示了Father类产生的实例的内存分布.father对象共占8个字节,前四个是虚函数表指针,后四个是int型的数据成员.而虚函数表里的内容是函数指针,负责对应虚函数的调用.
两种方法探索father对象的内存分布
下断点,运行,观察局部变量:
看下面的局部变量的窗口,很容易看出father对象里有两个数据,一个是void** 类型的二级指针(虚函数表指针),另一个是int类型的数据成员.自然而然得到前文的内存分布表.
VS中还有一个方式:
右键项目–属性
配置属性-- C/C++ --命令行–其他选项
添加命令:/d1 reportSingleClassLayout+类名
我这里的类名是Father,所以我添加的命令就是/d1 reportSingleClassLayoutFather
.然后重新生成,观察输出窗口.
虚函数表与单继承如何交互?
#include <iostream>
using namespace std;
class Father {
public:
virtual void function1() { cout << "调用了Father的方法1" << endl; }
virtual void function2() { cout << "调用了Father的方法2" << endl; }
private:
int x = 0;
};
class Son : public Father {
public:
void function1() { cout << "调用了Son的方法1" << endl; }
};
int main(void) {
Son son;
system("pause");
return 0;
}
更改代码并修改命令行,得son对象的内存分布:
进而得内存分布表
若子类中增加了虚函数,新的虚函数则会添加到虚函数表的最下面.
虚函数表与多继承如何交互?
#include <iostream>
using namespace std;
class Father {
public:
virtual void function1() { cout << "调用了Father的方法1" << endl; }
virtual void function2() { cout << "调用了Father的方法2" << endl; }
private:
int x = 0;
};
class Mother {
public:
virtual void func() { cout << "调用了Mother的方法" << endl; }
private:
int y = 0;
};
class Son : public Father, public Mother{
public:
void function1() { cout << "调用了Son的方法1" << endl; }
};
int main(void) {
Son son;
system("pause");
return 0;
}
得内存分布图,因为继承时Father类在上面,所以相应的虚函数表的排布也是这样.
多态
请看以下代码,它有何问题?
#include <iostream>
using namespace std;
class Father {
public:
Father() { cout << "调用了父类构造函数" << endl; }
virtual void func1() { cout << "调用了Father的方法1" << endl; }
//final关键字表示该虚函数不允许在子类内重写
virtual void func2() final { cout << "调用了Father的方法2" << endl; }
~Father() { cout << "调用了父类析构函数" << endl; }
};
class Son : public Father{
public:
Son() {
cout << "调用了子类构造函数" << endl;
x = new int;
}
//override关键字表示在子类内重写父类的功能,只能用在方法的定义,仅修饰虚函数
void func1() override { cout << "调用了Son的方法1" << endl; }
~Son() {
cout << "调用了子类析构函数" << endl;
delete x;
}
private:
int* x;
};
int main(void) {
Father* father = new Father;
father->func1();
father->func2();
delete father;
cout << endl;
Son* son = new Son;
son->func1();
delete son;
cout << endl;
father = new Son;
delete father;
system("pause");
return 0;
}
不幸,最后一段发生了内存泄漏.在子类有指针数据成员时使用多态竟然会导致这样的效果!子类的析构函数并没被调用,其数据成员当然也未释放内存.
当然,是可以解决的,只要把父类的析构函数改为虚函数即可.这个事故告诉我们,如果你正在写的类将来要作为父类,那么请将其析构函数设为虚析构函数.
#include <iostream>
using namespace std;
class Father {
public:
Father() { cout << "调用了父类构造函数" << endl; }
virtual void func1() { cout << "调用了Father的方法1" << endl; }
virtual void func2() final { cout << "调用了Father的方法2" << endl; }
virtual ~Father() { cout << "调用了父类析构函数" << endl; }
};
class Son : public Father{
public:
Son() {
cout << "调用了子类构造函数" << endl;
x = new int;
}
void func1() override { cout << "调用了Son的方法1" << endl; }
~Son() {
cout << "调用了子类析构函数" << endl;
delete x;
}
private:
int* x;
};
int main(void) {
Father* father = new Father;
father->func1();
father->func2();
delete father;
cout << endl;
Son* son = new Son;
son->func1();
delete son;
cout << endl;
father = new Son;
delete father;
system("pause");
return 0;
}
修改完毕,正常.