什么是构造函数
#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(Sclass
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 //可以看得出来 当当前函数执行完毕后,会调用析构函数
析构函数总结:
- 只能有一个析构函数,不能重载
- 不能带任何参数
- 不能带返回值
- 主要用于清理工作
- 编译器不要求必须提供
析构函数何时执行
- 当对象在堆栈中分配
当对象在堆栈中被清理的时候 就会执行,比如在函数中,那么离开函数就会被清理
- 当对象在全局区分配
当程序进程退出时会执行
析构函数的作用:主要用于清理工作
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); } };
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论