C++のvptrとvtable
773 ワード
// test.cpp : 。
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
class no_virtual {
int a;
public:
void x() const{}
int i() const {return 1;}
};
class one_virtual {
int a;
public:
virtual void x() const {}
int i() const {return 1;}
};
class two_virtuals{
int a;
public:
virtual void x() const {cout<(&two);
long* vpoint=(long*)(void_two[0]);
int x;
f=reinterpret_cast(vpoint[0]);
x=f();
f=reinterpret_cast(vpoint[1]);
x=f();
return 0;
}
出力:
int: 4 no_virtual: 4 void*: 4 one_virtual: 8 two_virtuals: 8 hello two_virtuals x() hello two_virtuals i()
このプログラムは、戻り値が関数を決定できないことを同時に説明します.