#include <iostream>
using namespace std;
class Test
{
public:
int a;
int b;
void print();
void s();
void ss();
void fs();
};
void Test::print()
{
//cout << "hello" << endl;
}
void Test::s()
{
}
void Test::ss()
{
}
void Test::fs()
{
}
void f()
{
int s = 3;
cout << "&s" << &s << endl;
cout << "f" << endl;
}
class B:Test
{
};
int main()
{
Test t;
cout << sizeof(t) << endl;
cout << &t.a << endl;
cout << &t.b << endl;
Test tt;
cout << &tt.a << endl;
cout << &tt.b << endl;
typedef void (Test::*p)(void);
////Test t;
////t.print();
p fun = &Test::print;
printf("%p\n",fun);
fun = &Test::s;
printf("%p\n",fun);
fun = &Test::ss;
printf("%p\n",fun);
fun = &Test::fs;
printf("%p\n",fun);
f();
cout << "--------" << endl;
B b;
cout << sizeof(b) << endl;
////((Test::*print)fun)();
//Test *p4;
//fun(p4);
// Test t;
// (t.*fun)();
// Test d;
// (d.*fun)();
}
類的成員函數(shù)不是在類實例化的時候載入內(nèi)存的。應(yīng)該是編譯鏈接的時候就在程序文件中確定了相對地址。然后程序載入的時候,所有的函數(shù)都載入到內(nèi)存的代碼區(qū)。所以實例化對象的時候,只是申請了對象的成員變量的地址,成員函數(shù)對于所有的類對象來說,應(yīng)該只有一份,在代碼區(qū)共用。而且類的成員變量和成員函數(shù)不是存放在一起(地址不是連續(xù)的,是分開存放的)
Test類中的a,b變量地址是連續(xù)的,每個占4字節(jié)。但是s(),ss(),fs()不是和a,b連續(xù)的,而且,這些函數(shù)之間,好像也并不是嚴格連續(xù)的,改一下函數(shù)名,地址有可能會變化