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++的更多相关文章

  1. Constructor Acquires, Destructor Releases Resource Acquisition Is Initialization

    w https://zh.wikipedia.org/wiki/RAII RAII要求,资源的有效期与持有资源的对象的生命期严格绑定,即由对象的构造函数完成资源的分配(获取),同时由析构函数完成资源的 ...

  2. __attribute__中constructor和destructor

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  3. TIJ——Chapter Five:Initialization & Cleanup

    Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...

  4. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization & Cleanup

    Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> ru ...

  5. __attribute__中constructor和destructor[总结]

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  6. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  7. 构建器Constructor的返回值/构建器

    构建器Constructor的返回值? 为什么会有这个问题? 在<Thinking in Java>中文Quanke翻译版本第四章初始化和清除,原书第五章Initialization&am ...

  8. Is it always safe to call getClass() within the subclass constructor?(转)

    14down votefavorite   An article on classloading states that the method getClass() should not be cal ...

  9. C++ 类 复制构造函数 The Copy Constructor

    一.复制构造函数的定义 复制构造函数是一种特殊的构造函数,具有一般构造函数的所有特性.复制构造函数创建一个新的对象,作为另一个对象的拷贝.复制构造函数只含有一个形参,而且其形参为本类对象的引用.复制构 ...

随机推荐

  1. 白帽子讲Web安全1.pdf

    第一章 我的安全世界观 安全是一个持续过程 6种威胁:Spoofing(伪装).Tampering(篡改).Repudiation(抵赖).InformationDisclosure(信息泄漏).De ...

  2. linux 双网关双IP设置

    server:CentOS5.8 ip:172.16.8.11 Gateway:172.16.8.1 ip:10.120.6.78 Gateway:10.120.6.1 网卡配置: eth0 poin ...

  3. C#如何将线程中的代码抛到主线程去执行

    private SynchronizationContext mainThreadSynContext; //主线程 mainThreadSynContext = new WindowsFormsSy ...

  4. hdoj 1827 Summer Holiday【强连通分量&&缩点】

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. 关于一个WCF调用的服务端和客户端的配置信息集合

    客户端的配置我知道. 但是: httpTransport maxReceivedMessageSize="2147483647" <dataContractSerialize ...

  6. canvas绘制简单小铅笔

    对应HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  7. Quartz定时任务学习(六)作业

    org.quartz.Job 接口 把 Quartz 作用到 Java 类上唯一要做的就是让它实现 org.quartz.Job 接口.你的 Job 类可以实现任何其他想要的接口或继承任何需要的基类, ...

  8. TortoiseSVN客户端重新设置用户名和密码[转]

    在第一次使用TortoiseSVN从服务器CheckOut的时候,会要求输入用户名和密码,这时输入框下面有个选项是保存认证信息,如果选了这个选项,那么以后就不用每次都输入一遍用户名密码了. 不过,如果 ...

  9. 使用 OpenSSL API 进行安全编程

    创建基本的安全连接和非安全连接 Kenneth Ballard ( kenneth.ballard@ptk.org), 自由程序员 Kenneth 是 Peru State College(位于 Pe ...

  10. [Javascript] MetaProgramming: function name

    Each function should have a 'name' property. It can be anonymous, empty, the same as function name, ...