zpcdcsdn 于 2016.01.13 11:31 提问

`````` #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;
}
``````

3个回答

zpcdcsdn   2016.01.13 19:35

cuiwei1026522829      2016.01.14 00:48

vs2013 和vs2015两个都装机器上，占地儿可不小。。

John_ToStr   2016.01.13 17:29

John_ToStr 回复zpcdcsdn: 我说过了，是编译器的问题，虚表是运行时的，并且是编译器相关的...

zpcdcsdn 非常感谢您的解答！其实之前我也在VS2013上测过，第一次运行结果是符合预期的，就像您上边的截图一样，但是，同样的程序。多次运行，就会出现和在VS2015上相似的结果。下面是我刚刚在VS2013上运行的结果截图。