#include <iostream>
using namespace std;
class Base {
public:
virtual void f() { cout << "Base::f" << endl; }
virtual void g() { cout << "Base::g" << endl; }
virtual void h() { cout << "Base::h" << endl; }
int age;
};
class Derive: public Base {
public:
virtual void f() { cout << "Derive::f" << endl; }
virtual void h() { cout << "Derive::h" << endl; }
};
int main()
{
Base b1;
Derive d1;
{
// 1、vptr在age成员之前, 为std::size_t字节
cout << "Base 对象的地址:" << (void*)&b1 << endl;
cout << "Base 对象member的地址:" << (void*) &b1.age << endl;
cout << "sizeof vptr:" << (uint8_t *) &b1.age - (uint8_t*)&b1 << endl;
}
std::cout << std::endl;
{
// 2、vtable的地址, vtable => std::size_t* vtable[N];
cout << "Base 对象 vtable member的地址:" << *(std::size_t *)&b1 << endl; // b1->vtable 10进制打印
cout << "Base 对象 vtable member的地址:" << (void *)*(std::size_t *)&b1 << endl; // b1->vtable 16进制打印
cout << "Derive 对象 vtable member的地址:" << *(std::size_t *)&d1 << endl; // d1->vtable
cout << "Derive 对象 vtable member的地址:" << (void *)*(std::size_t *)&d1 << endl;
}
std::cout << std::endl;
{
// 3、分别打印基类、子类vtable中函数地址打印
#define MemberFunNums 3
typedef std::size_t* Vtable;
typedef void(*FuncOfVtable)();
Vtable base_table = nullptr;
base_table = (Vtable)*(std::size_t *)&b1;
for (auto i = 0; i< MemberFunNums ; ++i) {
FuncOfVtable vFuncPtr = (FuncOfVtable)base_table[i];
std::cout << (void*) vFuncPtr << std::endl;
vFuncPtr();
}
base_table = (Vtable)*(std::size_t *)&d1;
for (auto i = 0; i< MemberFunNums ; ++i) {
FuncOfVtable vFuncPtr = (FuncOfVtable)base_table[i];
std::cout << (void*) vFuncPtr << std::endl;
vFuncPtr();
}
}
return 0;
}
#include <iostream>
#include <functional>
using namespace std;
class Base {
public:
int a;
virtual void g() {
cout << "Base::g" << endl;
}
virtual void h() {
cout << "Base::h" << endl;
}
virtual void f() {
cout << "Base::f" << endl;
}
};
class Derive : public Base {
public:
int a;
void print() {
cout << "print" << endl;
}
virtual void f1() {
cout << "Derive::f1" << endl;
}
virtual void f() {
cout << "Derive::f" << endl;
}
virtual void f(char a) {
cout << "Derive::f char a" << endl;
}
virtual void g() {
cout << "Derive::g" << endl;
}
virtual void h() {
cout << "Derive::h" << endl;
}
};
int main(int argc, char *argv[]) {
// typedef void(*pFun) (void);
using pFun = void (*)(void);
{
Base b;
cout << "虚函数表地址:" << (long *)(&b) << endl;
cout << "虚函数表 — 第一个函数地址:" << (long *)*(long *)(&b) << endl;
// Invoke the first virtual function,3种写法等价。。
pFun p1 = reinterpret_cast<pFun>(*(long *)(((long *)(&b))[0]));
auto p2 = (pFun) ((long *) * (long *)(&b))[1];
pFun p3 = (pFun) * ((long *) * (long *)(&b) + 2);
p1();
p2();
p3();
}
{
Derive d;
// 打印方法1
long **table = (long **)&d;
for (int i = 0; i < 7; ++i) {
auto pf = (pFun)table[0][i];
pf();
}
/* 打印方法2
for (int i = 0; i < 7; ++i) {
auto pf = (pFun)(long)((long *) * (long *)(&d))[i];
pf();
}
*/
}
{
// 虽然在Derive虚函数表中有 f1(),但是无法通过父类指针访问没有覆盖的函数
//Base1 *b1 = new Derive();
//b1->f1(); //编译出错
}
return 0;
}
🔗
备份地址: 【使用指针访问虚函数表】