测试代码
基类 base
,派生类 derived
,分别有成员变量、成员函数、虚函数
#include<stdio.h> #include<stdlib.h> class base { public: int a; double b; base() { this->a = 1; this->b = 2.3; printf("base constructor\n"); } void func() { printf("%d %lf\n", a, b); } virtual void v_func() { printf("base v_func()\n"); } ~base() { printf("base destructor\n"); } }; class derived :public base { public: derived() { printf("derived constructor\n"); } virtual void v_func() { printf("derived v_func()"); } ~derived() { printf("derived destructor\n"); } }; int main(int argc, char** argv) { base a; a.func(); a.v_func(); base* b = (base*)new derived(); b->func(); b->v_func(); return 0; }
编译: g++ test.cpp -o test
IDA视角
IDA打开,如下:
this指针
可以看到, base::
的每个函数都传入了一个参数 (base*)&v5
,正是类实例的 this指针
以下是普通成员函数 func()
的调用过程