老钟古

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

由汇编代码我们可以清楚的看到,C++中的函数重载对于非对象的重载基本上是一样的,只是在调用的时候采用不同的名称实现(VC++ 6.0实现)

程序的代码:

代码
 1 #include <iostream>
 2 using namespace std;
 3 
 4 
 5 void output( int x );
 6 void output( float x );
 7 
 8 void output( int x ) {
 9     cout << "output int " << x << endl;
10 }
11 
12 void output( float x ) {
13     cout << "output float " << x << endl;
14 }
15 
16 
17 int main() {
18 
19     int x = 1;
20     float y = 1.0;
21     output( x );
22     output( y );
23     output( 1 );
24     //output( 0.5 );
25     output( int0.5 ) );
26     output( float0.5 ) );
27 
28     return 0;
29 }
30 

 

 

 

两个函数重载的代码:

代码
 1 25:   void output( int x ) {
 2 004015A0   push        ebp
 3 004015A1   mov         ebp,esp
 4 004015A3   sub         esp,40h
 5 004015A6   push        ebx
 6 004015A7   push        esi
 7 004015A8   push        edi
 8 004015A9   lea         edi,[ebp-40h]
 9 004015AC   mov         ecx,10h
10 004015B1   mov         eax,0CCCCCCCCh
11 004015B6   rep stos    dword ptr [edi]
12 26:       cout << "output int " << x << endl;
13 004015B8   push        offset @ILT+200(std::endl) (004010cd)
14 004015BD   mov         eax,dword ptr [ebp+8]
15 004015C0   push        eax
16 004015C1   push        offset string "output int " (0046c01c)
17 004015C6   push        offset std::cout (004777f0)
18 004015CB   call        @ILT+655(std::operator<<) (00401294)
19 004015D0   add         esp,8
20 004015D3   mov         ecx,eax
21 004015D5   call        @ILT+255(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401104)
22 004015DA   mov         ecx,eax
23 004015DC   call        @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
24 27:   }
25 004015E1   pop         edi
26 004015E2   pop         esi
27 004015E3   pop         ebx
28 004015E4   add         esp,40h
29 004015E7   cmp         ebp,esp
30 004015E9   call        __chkesp (00420d20)
31 004015EE   mov         esp,ebp
32 004015F0   pop         ebp
33 004015F1   ret
34 
35 
36 
37 29:   void output( float x ) {
38 00401610   push        ebp
39 00401611   mov         ebp,esp
40 00401613   sub         esp,40h
41 00401616   push        ebx
42 00401617   push        esi
43 00401618   push        edi
44 00401619   lea         edi,[ebp-40h]
45 0040161C   mov         ecx,10h
46 00401621   mov         eax,0CCCCCCCCh
47 00401626   rep stos    dword ptr [edi]
48 30:       cout << "output float " << x << endl;
49 00401628   push        offset @ILT+200(std::endl) (004010cd)
50 0040162D   mov         eax,dword ptr [ebp+8]
51 00401630   push        eax
52 00401631   push        offset string "output float " (0046c02c)
53 00401636   push        offset std::cout (004777f0)
54 0040163B   call        @ILT+655(std::operator<<) (00401294)
55 00401640   add         esp,8
56 00401643   mov         ecx,eax
57 00401645   call        @ILT+305(std::basic_ostream<char,std::char_traits<char> >::operator<<) (00401136)
58 0040164A   mov         ecx,eax
59 0040164C   call        @ILT+490(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011ef)
60 31:   }
61 00401651   pop         edi
62 00401652   pop         esi
63 00401653   pop         ebx
64 00401654   add         esp,40h
65 00401657   cmp         ebp,esp
66 00401659   call        __chkesp (00420d20)
67 0040165E   mov         esp,ebp
68 00401660   pop         ebp
69 00401661   ret
70 
71 
72 

 

 

函数在进行调用的时候的不同名称:

 

代码
 1 38:       int x = 1;
 2 00401698   mov         dword ptr [ebp-4],1
 3 39:       float y = 1.0;
 4 0040169F   mov         dword ptr [ebp-8],3F800000h
 5 40:       output( x );
 6 004016A6   mov         eax,dword ptr [ebp-4]
 7 004016A9   push        eax
 8 004016AA   call        @ILT+260(output) (00401109)
 9 004016AF   add         esp,4
10 41:       output( y );
11 004016B2   mov         ecx,dword ptr [ebp-8]
12 004016B5   push        ecx
13 004016B6   call        @ILT+165(output) (004010aa)
14 004016BB   add         esp,4
15 42:       output( 1 );
16 004016BE   push        1
17 004016C0   call        @ILT+260(output) (00401109)
18 004016C5   add         esp,4
19 43:       //output( 0.5 );
20 44:       output( int0.5 ) );
21 004016C8   push        0
22 004016CA   call        @ILT+260(output) (00401109)
23 004016CF   add         esp,4
24 45:       output( float0.5 ) );
25 004016D2   push        3F000000h
26 004016D7   call        @ILT+165(output) (004010aa)
27 004016DC   add         esp,4

 


 

 

 

posted on 2010-09-24 20:07  老钟古  阅读(298)  评论(0编辑  收藏  举报