Last active
July 15, 2017 02:56
-
-
Save FelixZhang00/173be139404ec703a47dd4d1e52137ad to your computer and use it in GitHub Desktop.
C++反汇编 虚函数的例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```c++ | |
#include <stdio.h> | |
class BaseClass { | |
public: | |
BaseClass(){x=1;y=2;}; | |
virtual void vfunc1() = 0; | |
virtual void vfunc2(){}; | |
virtual void vfunc3(); | |
virtual void vfunc4(){}; | |
void hello(){printf("hello,y=%d",this->y);}; | |
private: | |
int x;//4字节 | |
int y; | |
}; | |
class SubClass : public BaseClass { | |
public: | |
SubClass(){z=3;}; | |
virtual void vfunc1(){}; | |
virtual void vfunc3(); | |
virtual void vfunc5(){}; | |
private: | |
int z; | |
}; | |
void BaseClass::vfunc3(){ | |
printf("BaseClass::vfunc3"); | |
} | |
void SubClass::vfunc3(){ | |
printf("SubClass::vfunc3"); | |
} | |
void call_vfunc(BaseClass *a) { | |
a->vfunc3(); | |
} | |
int main() { | |
//①调用new操作符,在堆上动态分配一块SubClass大小的内存,rax指向这块内存的开始 | |
//②rax的值作为参数调用SubClass构造函数 | |
BaseClass *a_ptr = new SubClass(); | |
//对象首地址作为参数调用函数call_vfunc | |
call_vfunc(a_ptr); | |
//一般的成员函数,不在虚表里 | |
a_ptr->hello(); | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment