C++的class由C的srtuct變化而來,先來看看兩個地方有什么區別:
1.C++代碼
1
#include "iostream"
2
using namespace std;
3
4
struct A
5

{
6
int a;
7
void display(int s);
8
};
9
10
void A::display(int s)
11

{
12
a = 1;
13
cout<<"this is in A:"<<s<<" "<<a<<endl;
14
}
15
16
class B
17

{
18
public:
19
int b;
20
void display(int s);
21
};
22
23
void B::display(int s)
24

{
25
b = 2;
26
cout<<"this is in B:"<<s<<" "<<b<<endl;
27
}
28
29
void main()
30

{
31
A a;
32
a.display(sizeof(a));
33
34
B b;
35
b.display(sizeof(b));
36
}
37
2.匯編代碼:
1.debug編譯
1
.text:00401820 main proc near ; CODE XREF: _mainj
2
.text:00401820
3
.text:00401820 var_48 = dword ptr -48h
4
.text:00401820 var_8 = dword ptr -8
5
.text:00401820 var_4 = dword ptr -4
6
.text:00401820
7
.text:00401820 push ebp
8
.text:00401821 mov ebp, esp
9
.text:00401823 sub esp, 48h
10
.text:00401826 push ebx
11
.text:00401827 push esi
12
.text:00401828 push edi
13
.text:00401829 lea edi, [ebp+var_48]
14
.text:0040182C mov ecx, 12h
15
.text:00401831 mov eax, 0CCCCCCCCh
16
.text:00401836 rep stosd
17
.text:00401838 push 4
18
.text:0040183A lea ecx, [ebp+var_4]
19
.text:0040183D call j_A__display
20
.text:00401842 push 4
21
.text:00401844 lea ecx, [ebp+var_8]
22
.text:00401847 call j_B__display
23
.text:0040184C pop edi
24
.text:0040184D pop esi
25
.text:0040184E pop ebx
26
.text:0040184F add esp, 48h
27
.text:00401852 cmp ebp, esp
28
.text:00401854 call __chkesp
29
.text:00401859 mov esp, ebp
30
.text:0040185B pop ebp
31
.text:0040185C retn
32
.text:0040185C main endp
33
2.release編譯
.text:00401140 ; int __cdecl main(int argc,const char **argv,const char *envp)
.text:00401140 _main proc near ; CODE XREF: start+AFp
.text:00401140
.text:00401140 var_8 = dword ptr -8
.text:00401140 var_4 = dword ptr -4
.text:00401140 argc = dword ptr 4
.text:00401140 argv = dword ptr 8
.text:00401140 envp = dword ptr 0Ch
.text:00401140
.text:00401140 sub esp, 8
.text:00401143 lea ecx, [esp+8+var_8]
.text:00401147 push 4
.text:00401149 call sub_401000
.text:0040114E push 4
.text:00401150 lea ecx, [esp+0Ch+var_4]
.text:00401154 call sub_4010A0
.text:00401159 add esp, 8
.text:0040115C retn
.text:0040115C _main endp

3.輸出結果
this is in A:4 1
this is in B:4 2
4.結論
1.struct和class沒有任何區別,他們在代碼段有一個"模板"
2.對象占用的4個字節是int的大小
3.函數在代碼中定義,由編譯器決定調用誰
posted on 2008-03-02 21:16
margin 閱讀(474)
評論(0) 編輯 收藏 引用 所屬分類:
C/C++ 、
逆向工程