c++ 虚函数地址
最近在看《深度探索C++对象模型》这本书,看到类继承的对象布局那里,就写了如下的代码来玩玩看
#include<stdio.h>#include<string.h>class Person{public: Person(char *name, int age); ~Person();public: char * GetName() const {return (char*)&this->m_name;} int GetAge() const {return this->m_age;} virtual void Say();protected: char m_name[20]; int m_age;};Person::Person(char *name, int age){ strcpy(this->m_name, name); this->m_age = age;}Person::~Person(){ //}void Person::Say(){ printf("My name is %s, I'm %d this year.\n", this->m_name, this->m_age);}//////////////////////////////////////////////////////////////////////////class Student : public Person{public: Student(char *name, int age, int id); ~Student();public: virtual void Say();protected: int m_id;};Student::Student(char *name, int age, int id) : Person(name, age){ this->m_id = id;}Student::~Student(){}void Student::Say(){ printf("I am a student by the name of %s, I'm %d, my id is %d.\n", this->m_name, this->m_age, this->m_id);}int main(){ Person pa("pa", 12); Student s("st", 20, 2008); Person *ps = &s; pa.Say(); s.Say(); ps->Say(); printf("&pa:%0x\n", &pa); printf(" &s:%0x\n", &s); printf(" ps:%0x\n", ps); printf("pa.say:%0x\n", pa.Say); printf(" s.say:%0x\n", s.Say); printf("ps.say:%0x\n", ps->Say); return 0;}0040EE8D push offset @ILT+35(`vcall') (00401028)0040EE92 push offset string "pa.say:%0x\n" (004260a0)0040EE97 call printf (004014c0)0040EE9C add esp,80040EE9F push offset @ILT+35(`vcall') (00401028)0040EEA4 push offset string " s.say:%0x\n" (00426094)0040EEA9 call printf (004014c0)0040EEAE add esp,80040EEB1 push offset @ILT+35(`vcall') (00401028)0040EEB6 push offset string "ps.say:%0x\n" (00425084)0040EEBB call printf (004014c0)0040EEC0 add esp,8
00401028 E9 83 00 00 00 CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC 閮...烫烫烫烫烫烫烫.0040103C CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC 烫烫烫烫烫烫烫烫烫烫00401050 55 8B EC 83 EC 44 53 56 57 51 8D 7D BC B9 11 00 00 00 B8 CC U嬱冹DSVWQ峿脊....柑00401064 CC CC CC F3 AB 59 89 4D FC 8B 45 FC C7 00 1C 50 42 00 8B 4D 烫腆玒塎鼖E..PB.婱00401078 08 51 8B 55 FC 83 C2 04 52 E8 0A 03 00 00 83 C4 08 8B 45 FC .Q婾鼉..R.....兡.婨.