posts - 101,  comments - 57,  trackbacks - 0

     C++的class由C的srtuct变化而来,先来看看两个地方有什么区别:

1.C++代码

 1#include "iostream"
 2using namespace std;
 3
 4struct A
 5{
 6    int a;
 7    void display(int s);
 8}
;
 9
10void A::display(int s)
11{
12    a = 1;
13    cout<<"this is in A:"<<s<<" "<<a<<endl;
14}

15
16class B
17{
18public:
19    int b;
20    void display(int s);
21}
;
22
23void B::display(int s)
24{
25    b = 2;
26    cout<<"this is in B:"<<s<<" "<<b<<endl;
27}

28
29void 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 阅读(436) 评论(0)  编辑 收藏 引用 所属分类: C/C++逆向工程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔档案

文章分类

文章档案

收藏夹

常去的坛子

  • CVC电脑病毒论坛
  • 很多人说我是AV,我告诉他们:别瞧不起人,我们也能创造价值
  • 安全焦点
  • 黑客聚集的地方,一般是好酒最多的地方...
  • 看雪论坛
  • 国内最强的加密解密论坛,成醉其中经常夜不归宿
  • 驱动开发论坛
  • 厌倦了啤的朋友们,来我们来整点白的...痛痛快快的BSOD也好过隔鞋瘙痒!

我的朋友

  • Sen的blog
  • IDE方面资深的受害者...经常为一个变量的定义找不着北的痛苦程序员(深表同情)
  • 老罗的blog
  • 良师益友,千年水牛,引擎猛男,分析怪兽,墨镜酷哥,台球高手....

搜索

  •  

最新评论