C++ 虚函数表
/**
* @file morevir.cpp
* @brief 对于包含虚函数的类,不管有多少个虚函数,只有一个虚指针,vptr的大小。 * @author Ernest
* @version v1
* @date 2019-07-21
*/
#include<iostream>
using namespace std;
int i = 0;
class A{
public:
virtual void fun(){i++;this->fun3();};
virtual void fun1(){this->fun2();};
virtual void fun2(){i++;};
virtual void fun3(){i++;this->fun1();};
virtual ~A(){};
};
int main() {
class A *a = new A();
a->fun();
a->fun3();
cout<<sizeof(*a)<<endl; // 8
cout<<i<<endl;
delete a;
return i;
}
//8
//5

查看虚函数表发现A有两个析构函数!

代码中调用的是第二个析构函数!
The entries for virtual destructors are actually pairs of entries. The first destructor, called the complete object destructor, performs the destruction without calling delete() on the object. The second destructor, called the deleting destructor, calls delete() after destroying the object.