下面是在下的代码:
#include <iostream> using namespace std; class Point { private: int X; int Y; public: Point() {} Point(int x, int y) : X(x), Y(y) {} const int getX() { return X; } const int getY() { return Y; } virtual ~Point() {} virtual void printX() { cout << "X=" << X << endl; } virtual void printY() { cout << "Y=" << Y << endl; } }; class Point3D :public Point { private: int Z; public: Point3D() :Point() {} Point3D(int x, int y, int z) :Point(x, y), Z(z) {} const int getZ() { return Z; } virtual ~Point3D() {} virtual void printZ() { cout << "Z=" << Z << endl; } virtual void fun() { cout << "fun" << endl; } }; int main() { typedef void(*Funptr)(); Point point(1, 2); Point3D point3D(1, 2, 3); Point3D* p3D = &point3D; void*** _p3D = (void***)&point3D; void** vfptr3D = *_p3D; int** ptr3D = (int**)vfptr3D; while (*ptr3D != NULL) { cout << *ptr3D << endl; ptr3D++; } cout << "--------------------------" << endl; Point* p = &point; void*** _p = (void***)&point; void** vfptr = *_p; int** ptr = (int**)vfptr; while (*ptr != NULL) { cout << *ptr << endl; ptr++; } return 0; }
在上面的代码中,在下打印了派生类和基类的虚函数表,结果如下
按理说,基类的虚函数表只有三项,分别对应~point(),printX(),printY(),如下图
但大家也发现了,打印出的结果中最后多了一项!这是在VS2015下的结果,如果在codeblocks12.11下,连派生类的虚函数表最后也会多出一项,如下
但是,只要去掉虚析构函数,就不会出现意料之外的多余项,可见出现多余项与类中含有虚析构函数有关,但在下又想不明白这是为什么,所以特向大家请教,希望各路大神能够不吝赐教,在下先行谢过!