示例:
#include "stdafx.h"
#include <Windows.h>
class Car
{
public:
virtual void Run(){}
private:
int m_speed;
};
struct  SRace
{
Car car;
int dirver;
};
int _tmain(int argc, _TCHAR* argv[])
{
SRace race;
ZeroMemory(&race,sizeof(race));
// 没通过虚表指针调用,没事
race.car.Run();
Car *pCar = &race.car;
// __vfptr = 0x00000000,崩溃
pCar->Run();
return 0;
}
[cpp] view plaincopy
#include "stdafx.h"
#include <Windows.h>
class Car
{
public:
virtual void Run(){}
private:
int m_speed;
};
struct  SRace
{
Car car;
int dirver;
};
int _tmain(int argc, _TCHAR* argv[])
{
SRace race;
ZeroMemory(&race,sizeof(race));
// 没通过虚表指针调用,没事
race.car.Run();
Car *pCar = &race.car;
// __vfptr = 0x00000000,崩溃
pCar->Run();
return 0;
}
={0}
  ={0}操作是结构体和数组的一种初始化方式,它是将结构体中基本类型变量赋默认值,当结构体中有非基本类型(例如类对象)时,会编译错误,这也是一种保护。