#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;
{
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;
{
cout << "Base 对象 vtable member的地址:" << *(std::size_t *)&b1 << endl;
cout << "Base 对象 vtable member的地址:" << (void *)*(std::size_t *)&b1 << endl;
cout << "Derive 对象 vtable member的地址:" << *(std::size_t *)&d1 << endl;
cout << "Derive 对象 vtable member的地址:" << (void *)*(std::size_t *)&d1 << endl;
}
std::cout << std::endl;
{
#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[]) {
using pFun = void (*)(void);
{
Base b;
cout << "虚函数表地址:" << (long *)(&b) << endl;
cout << "虚函数表 — 第一个函数地址:" << (long *)*(long *)(&b) << endl;
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;
long **table = (long **)&d;
for (int i = 0; i < 7; ++i) {
auto pf = (pFun)table[0][i];
pf();
}
}
{
}
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4f296ce8ec051491006b409f64ef01db.png)