class Person { public: Person():mId(0), mAge(20){} void print() { cout << "id: " << mId << ", age: " << mAge << endl; } private: int mId; int mAge; };
int main() { Person p1; cout << "sizeof(p1) == " << sizeof(p1) << endl; int *p = (int*)&p1; cout << "p.id == " << *p << ", address: " << p << endl; ++p; cout << "p.age == " << *p << ", address: " << p << endl; cout << endl; Person p2; cout << "sizeof(p2) == " << sizeof(p1) << endl; p = (int*)&p2; cout << "p.id == " << *p << ", address: " << p << endl; ++p; cout << "p.age == " << *p << ", address: " << p << endl; return 0; }
class Person { public: Person():mId(0), mAge(20){ ++sCount; } ~Person(){ --sCount; } void print() { cout << "id: " << mId << ", age: " << mAge << endl; } static int personCount() { return sCount; } private: static int sCount; int mId; int mAge; };
class Person { public: Person():mId(0), mAge(20){ ++sCount; } static int personCount() { return sCount; } virtual void print() { cout << "id: " << mId << ", age: " << mAge << endl; } virtual void job() { cout << "Person" << endl; } virtual ~Person() { --sCount; cout << "~Person" << endl; } protected: static int sCount; int mId; int mAge; };
int main() { Person person; cout << sizeof(person) << endl; int *p = (int*)&person; for (int i = 0; i < sizeof(person) / sizeof(int); ++i, ++p) { cout << *p << endl; } return 0; }
typedef void (*FuncPtr)(); int main() { Person person; int **vtbl = (int**)*(int**)&person; for (int i = 0; i < 3 && *vtbl != NULL; ++i) { FuncPtr func = (FuncPtr)*vtbl; func(); ++vtbl; } while (*vtbl) { cout << "*vtbl == " << *vtbl << endl; ++vtbl; } return 0; }
Person person; int **p = (int**)&person;
int **vtbl = (int**)*p;
FuncPtr func = (FuncPtr)*vtbl; func();
原文地址:http://blog.csdn.net/ljianhui/article/details/45903939