首页 > 编程语言 > 详细

Constructor and destructor -- Initialization & Cleanup in C++

时间:2014-05-08 19:11:34      阅读:383      评论:0      收藏:0      [点我收藏+]

Why need initialization and cleanup?

A large segment of C bugs occur when the programmer forgets to initialize or clean up a variable.

The class designer can guarantee initialization of every object by providing a special function called the constructor. If a class has a constructor, the compiler automatically calls that constructor at the point an object is created, before client programmers can get their hands on the object. The constrctor call isnot even an option for the client programmer, it is performed by the compiler at the point the object is defined.

 

Default constructors 

a default constructor is ont that can be called with no arguments. A default constructor is used to create a "vanilla object". The default constructor is so important that if and only if there are no constructors for a structure(struct or class), the compiler will automatically create one for you.

For example

class V{

  int i;

};

void main()

{

  V v, v2[10];

}

It is OK.

But if any constructors are defined, however, and there‘s no default constructor, the instances of V above will generate compile-time errors.

Constructor and destructor -- Initialization & Cleanup in C++,布布扣,bubuko.com

Constructor and destructor -- Initialization & Cleanup in C++

原文:http://www.cnblogs.com/ruccsbingo/p/3713764.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!