博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
构造函数与析构函数之间的区别
阅读量:2509 次
发布时间:2019-05-11

本文共 3453 字,大约阅读时间需要 11 分钟。

Constructor and Destructor are the special methods which makes our crucial tasks easier while programming. If you have ever worked in programming language like C++ then you must have encountered these two terms. Being methods, they clearly let us implement the reusability of the code. But the talk of the town (esp. for the beginners) has clearly been the differences among them, their types, usage, etc. The somewhat rhyming names they possess and both being simply the types of the method or function makes it even more difficult for a beginner to understand the difference.

构造函数和析构函数是特殊的方法,可以使我们在编程时更轻松地完成关键任务。 如果您曾经使用过像C ++这样的编程语言,那么您一定遇到过这两个术语。 作为方法,它们显然让我们实现了代码的可重用性。 但是该镇(尤其是初学者)的话题显然是它们之间的区别,它们的类型,用途等。它们所拥有的名称有些押韵,而它们只是方法或功能的类型,因此变得更加困难让初学者了解差异。

Here is a simple article, which clearly differentiates the two is sure to strike the right the chord in a programmer’s mind.

这是一篇简单的文章,清楚地区分了两者,确保在程序员的脑海中触及正确的共鸣。

构造函数与析构函数之间的区别 (Difference Between Constructor and Destructor)

S.no. Parameter of Difference Constructor Destructor
1. Initialization Initialized with the same as that of the class name. i.e.,

Example: if the class name is maxim, the constructor for the same will be  maxim()

Also initialized with the same name as that of the class name, but with a tilde (~) sign before the class name.

Example: if the class name is maxim, the constructor for the same will be ~maxim()

2. Objective Creates the class objects.

Or, When a constructor is called, it allocates the memory.

Used for destroying the objects. i.e., when a destructor is called, memory is released.
3. Parameters/arguments Accepts the arguments.

Example: maxim(a,b,c)

Does not contain any arguments.

Example : maxim()

4. Overloading Posses Overloading property. Multiple constructors exist in a class at a time. Does not possess the property of overloading; (Also there is no need of destructor overloading). Only one destructor can exist in one class at a time.
5. Invoking As soon as the object is created, a constructor is called by default. Also, they are called in successive order; in the order they are designed. When the concerned block is encountered or if the program is terminated, Destructor is called. They are called in the reverse order of the design.
6. Types Constructor can be of types: Default (with no arguments) and parameterized (with arguments). Destructor can never be parameterized (as mentioned earlier).
7. Copying the values The copy constructor lets us declare and initialize the object from some other object. There is no like functionality in the Destructor.
8. Syntax Class_name(a,b,c)

{

//body of the constuctor

}

~Class_name()

{

//body of the destructor

}

序号 差异参数 建设者 析构函数
1。 初始化 用与类名相同的名称进行初始化。 即

示例:如果类名称为maxim ,则该类的构造函数为maxim()

还使用与类名称相同的名称进行了初始化,但是在类名称之前使用了波浪号( )。

示例:如果类名称为maxim ,则其构造函数为〜maxim()

2。 目的 创建类对象。

或者,当调用构造函数时,它将分配内存。

用于破坏物体。 也就是说,当调用析构函数时,将释放内存。
3。 参数/参数 接受参数。

示例:maxim(a,b,c)

不包含任何参数。

示例:maxim()

4。 超载 拥有重载属性。 一个类中一次存在多个构造函数。 不具有过载的特性; (而且,不需要析构函数重载)。 一次只能在一个类中存在一个析构函数。
5, 调用中 创建对象后,默认情况下会调用构造函数。 同样,它们以连续的顺序被调用。 按照设计顺序。 当遇到有关的块或程序终止时,将调用Destructor。 以设计的相反顺序调用它们。
6。 种类 构造函数的类型可以是:默认(不带参数)和参数化(带参数)。 析构函数永远无法参数化(如前所述)。
7 复制值 复制构造函数使我们可以从其他对象声明并初始化该对象。 析构函数中没有类似的功能。
8。 句法 类名(a,b,c)

{

//构造器的主体

}

〜Class_name()

{

//析构函数的主体

}

However similar they may sound, constructors and destructors are quite a pole apart and this can be easily perceived from the above mentioned differences.

无论听起来如何相似,构造函数和析构函数相距甚远,这可以从上述差异中轻松看出。

翻译自:

转载地址:http://daggb.baihongyu.com/

你可能感兴趣的文章
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>
MySQL 5.1参考手册
查看>>
TensorFlow安装流程(GPU加速)
查看>>
OpenStack的容器服务体验
查看>>
【BZOJ 4059】 (分治暴力|扫描线+线段树)
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>
linux 内核的优化
查看>>
近端梯度算法(Proximal Gradient Descent)
查看>>
DRM-内容数据版权加密保护技术学习(中):License预发放实现 (转)
查看>>
TCP与UDP协议
查看>>
php上传文件如何保证上传文件不被改变或者乱码
查看>>
目录遍历代码
查看>>
iOS MD5加密实现方法
查看>>