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. codeforces Codeforces 650A Watchmen

    题意:两点(x1,y1), (x2,y2)的曼哈顿距离=欧几里得距离 也就是:x1=x2或y1=y2,再删除重合点造成的重复计数即可. #include <stdio.h> #includ ...

  2. phpnow下mysqli加载不了的问题

    用了phpnow也有蛮长一段时间了.一直感觉还不错,不过就是差点把手动配置apache/php/mysql环境的方法都给忘了. 这几天一直都在找crm系统,可惜没有几个满意的.今天逛上了weberp这 ...

  3. Eclipse非常有用的快捷键

    Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的快捷键操作,并且精心录制了动画, 让你一看就会. ...

  4. js select级联,上面分类,下面是内容

    js select级联,上面分类,下面是内容. js级联效果如下: 分类: 请选择 水果 蔬菜 其他 内容: // html和js代码如下:     <html>      <hea ...

  5. final效率分析

    1. 被final修饰的类 public final class FinalClass { public void getJava() { String str1 = "Java " ...

  6. SAP HANA STRING_AGG

    HANA Version 1.00.73.00.389160 不支持STRING_AGG,所以只能,,,,,,,, DROP PROCEDURE ""."ZCONCAT_ ...

  7. UVA Mapping the Swaps

    题目例如以下: Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entrie ...

  8. C# 如何设置 richTextBoxr的边距

    附件 http://files.cnblogs.com/xe2011/richTextBox_EM_SETRECT.rar using System.Runtime.InteropServices; ...

  9. Quartz Enterprise Job Scheduler 1.x Tutorial---转载

    Lesson 10: Configuration, Resource Usage and SchedulerFactory http://www.quartz-scheduler.org/docume ...

  10. JSPatch 成长之路

    在这次 GMTC 大会上,我见到了 JSPatch 的作者 bang.在这之前我就和他在网上认识并聊过很多次,bang 也在这个公众号上投稿发表了多篇关于 JSPatch 的文章,包括:JSPatch ...