C++ 中的vptr与vtable

// 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<<"hello two_virtuals x()"<(&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()

该程序同时能说明,返回值不能确定一个函数。

你可能感兴趣的:(C/C++,C语言复习要点)