Cpp2 构造函数和析构函数

admin 2018年5月13日03:41:11评论577 views字数 1284阅读4分16秒阅读模式
摘要

总结特点/总结特点">Person) (00401005) 00401069 mov dword ptr [ebp-4],0 00401070 lea ecx,[ebp-14h] 00401073 call @ILT+10(Person::Print) (0040100f) 00401078 mov dword ptr [ebp-18h],0 0040107F mov dword ptr [ebp-4],0FFFFFFFFh 00401086 lea ecx,[ebp-14h] 00401089 call @ILT+5(Person::~Person) (0040100a) 0040108E mov eax,dword ptr [ebp-18h] 00401091 mov ecx,dword ptr [ebp-0Ch] 00401094 mov dword ptr fs:[0],ecx //可以看得出来 当当前函数执行完毕后,会调用析构函数


什么是构造函数

#include "stdafx.h" #include <stdio.h>  struct Sclass {     int a;     int b;     int c;     int d;     Sclass()//构造函数     {         printf("观察这个函数 /n");     }     Sclass(int a,int b,int c,int d)//构造函数     {         this->a=a;         this->b=b;         this->c=c;         this->d=d;         printf("观察这个函数 2/n");     }     int Plus()     {         return a+b+c+d;     } };  int main(int argc, char* argv[]) {     Sclass s;     Sclass s2(1,2,3,4);      return 0; }  //反汇编:  Sclass s; 0040D408   lea         ecx,[ebp-10h] 0040D40B   call        @ILT+5(Sclass::Sclass) (0040100a)  Sclass s2(1,2,3,4); 0040D770   push        4 0040D772   push        3 0040D774   push        2 0040D776   push        1 0040D778   lea         ecx,[ebp-20h] 0040D77B   call        @ILT+10(SclassPerson) (00401005) 00401069   mov         dword ptr [ebp-4],0 00401070   lea         ecx,[ebp-14h] 00401073   call        @ILT+10(Person::Print) (0040100f) 00401078   mov         dword ptr [ebp-18h],0 0040107F   mov         dword ptr [ebp-4],0FFFFFFFFh 00401086   lea         ecx,[ebp-14h] 00401089   call        @ILT+5(Person::~Person) (0040100a) 0040108E   mov         eax,dword ptr [ebp-18h] 00401091   mov         ecx,dword ptr [ebp-0Ch] 00401094   mov         dword ptr fs:[0],ecx  //可以看得出来 当当前函数执行完毕后,会调用析构函数

析构函数总结:

  1. 只能有一个析构函数,不能重载
  2. 不能带任何参数
  3. 不能带返回值
  4. 主要用于清理工作
  5. 编译器不要求必须提供

析构函数何时执行

  1. 当对象在堆栈中分配 当对象在堆栈中被清理的时候 就会执行,比如在函数中,那么离开函数就会被清理
  2. 当对象在全局区分配 当程序进程退出时会执行

析构函数的作用:主要用于清理工作

struct Person {     int age;     int level;     char* arr;     Person()     {         printf("无参构造函数执行了。。/n");     }     Person(int age,int level)     {         printf("有参构造函数执行了 ../n");         this->age=age;         this->level=level;         arr=(char*)malloc(1024); //申请堆内存     }     ~Person()     {         printf("析构函数执行了!!/n");         free(arr);//释放堆内存     }     void Print()     {         printf("%d - %d /n",age,level);     } };

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2018年5月13日03:41:11
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Cpp2 构造函数和析构函数http://cn-sec.com/archives/51518.html

发表评论

匿名网友 填写信息