64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class CTest
{
public:
static void sfun();
void nsfun();
private:
int a;
};
void CTest::sfun()
{
cout<<"in static fun"<<endl;
}
void CTest::nsfun()
{
a = 2;
cout<<"in nonstatic fun"<<endl;
}
void main()
{
CTest test1;
test1.nsfun();
CTest::sfun();
test1.sfun();
CTest test2;
test2.nsfun();
}
void main()
{
004115D0 push ebp
004115D1 mov ebp,esp
004115D3 sub esp,0D8h
004115D9 push ebx
004115DA push esi
004115DB push edi
004115DC lea edi,[ebp-0D8h]
004115E2 mov ecx,36h
004115E7 mov eax,0CCCCCCCCh
004115EC rep stos dword ptr es:[edi]
CTest test1;
test1.nsfun();
004115EE lea ecx,[test1]
004115F1 call CTest::nsfun (4111C7h)
CTest::sfun();
004115F6 call CTest::sfun (41111Dh)
test1.sfun();
004115FB call CTest::sfun (41111Dh) ;两种调用静态成员函数的地址是相同的。
CTest test2;
test2.nsfun();
00411600 lea ecx,[test2]
00411603 call CTest::nsfun (4111C7h) ;虽然函数地址与004115F1相同,但是ecx
;即this不同。说明非静态成员函数与静态成员函数的差别就在于调用是是否隐式地传入了this。
}
...;省略若干行