当前位置:编程学习 > C/C++ >>

用汇编的眼光看C++(之拷贝、赋值函数)

 

     【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

 

 

 

 

    拷贝构造函数和复制函数是类里面比较重要的两个函数。两者有什么区别呢?其实也很简单,我们可以举个例子,加入有这样一个类的定义:

 

 

copy to clipboardprint?class apple 

public: 

    apple() {  printf("apple()!\n");} 

    apple(apple& a) {  printf("copy apple()!\n");} 

    apple& operator=(apple& a) {  printf("= apple()\n"); return *this;} 

    ~apple() {  printf("~apple()!\n");} 

    void print() const {  return;} 

}; 

class apple

{

public:

       apple() {  printf("apple()!\n");}

       apple(apple& a) {  printf("copy apple()!\n");}

       apple& operator=(apple& a) {  printf("= apple()\n"); return *this;}

       ~apple() {  printf("~apple()!\n");}

       void print() const {  return;}

};

 

 

    那么我们在如下的函数里面进行调用的时候,调用的函数分别是哪些呢?

 

 

copy to clipboardprint?void process() 

    apple a, c; 

    apple b =a; 

    c = b; 

void process()

{

       apple a, c;

       apple b =a;

       c = b;

}    其实汇编的结果是这样的,大家可以一起看一下,自己尝试读一下。如果一次不是很明白,可以多读几次。

copy to clipboardprint?70:       apple a, c; 

0040127D   lea         ecx,[ebp-10h] 

00401280   call        @ILT+70(apple::apple) (0040104b) 

00401285   mov         dword ptr [ebp-4],0 

0040128C   lea         ecx,[ebp-14h] 

0040128F   call        @ILT+70(apple::apple) (0040104b) 

00401294   mov         byte ptr [ebp-4],1 

71:       apple b =a; 

00401298   lea         eax,[ebp-10h] 

0040129B   push        eax 

0040129C   lea         ecx,[ebp-18h] 

0040129F   call        @ILT+50(apple::apple) (00401037) 

004012A4   mov         byte ptr [ebp-4],2 

72:       c = b; 

004012A8   lea         ecx,[ebp-18h] 

004012AB   push        ecx 

004012AC   lea         ecx,[ebp-14h] 

004012AF   call        @ILT+75(apple::operator=) (00401050) 

73:   } 

004012B4   mov         byte ptr [ebp-4],1 

004012B8   lea         ecx,[ebp-18h] 

004012BB   call        @ILT+0(apple::~apple) (00401005) 

004012C0   mov         byte ptr [ebp-4],0 

004012C4   lea         ecx,[ebp-14h] 

004012C7   call        @ILT+0(apple::~apple) (00401005) 

004012CC   mov         dword ptr [ebp-4],0FFFFFFFFh 

004012D3   lea         ecx,[ebp-10h] 

004012D6   call        @ILT+0(apple::~apple) (00401005) 

004012DB   mov         ecx,dword ptr [ebp-0Ch] 

004012DE   mov         dword ptr fs:[0],ecx 

004012E5   pop         edi 

004012E6   pop         esi 

004012E7   pop         ebx 

004012E8   add         esp,58h 

004012EB   cmp         ebp,esp 

004012ED   call        __chkesp (004087c0) 

004012F2   mov         esp,ebp 

004012F4   pop         ebp 

004012F5   ret 

70:       apple a, c;

0040127D   lea         ecx,[ebp-10h]

00401280   call        @ILT+70(apple::apple) (0040104b)

00401285   mov         dword ptr [ebp-4],0

0040128C   lea         ecx,[ebp-14h]

0040128F   call        @ILT+70(apple::apple) (0040104b)

00401294   mov         byte ptr [ebp-4],1

71:       apple b =a;

00401298   lea         eax,[ebp-10h]

0040129B   push        eax

0040129C   lea         ecx,[ebp-18h]

0040129F   call        @ILT+50(apple::apple) (00401037)

004012A4   mov         byte ptr [ebp-4],2

72:       c = b;

004012A8   lea         ecx,[ebp-18h]

004012AB   push        ecx

004012AC   lea         ecx,[ebp-14h]

004012AF   call        @ILT+75(apple::operator=) (00401050)

73:   }

004012B4   mov         byte ptr [ebp-4],1

004012B8   lea         ecx,[ebp-18h]

004012BB   call

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,