Members that are const or reference must be initialized. Similary, members that are of a class type that does not define a default constructor also must be initialized.

class ConstRef{
public:
ConstRef(int ii);
private:
int i;
const int ci;
int & ri;
}; ConstRef::ConstRef(int ii){
i = ii; // ok
ci = ii; // error: cannot assign to a const
ri = i; // error: ri was never initialized
}

By the time the constructor begin executing, initialization is completed. The correct way to wirtie this constructor is:

// explictly initialize reference and const members
ConstRef::ConstRef(int ii): i(ii), ci(ii), ri(i) { }

Members are initiazlied in the order in which they appear in the class definition, not the order in which they appear in the constructor initializer.

class X{
int i;
int j;
public:
// undefined: i is initialized before j
X(int val): j(val), i(j) { }
}

To avoid using members to initiazlie other members.

    X(int val): j(val), i(val) { }
class Sales_data{
public:
// non-delegating constructor initialize member before corresponding argument
Sales_data(string s, unsigned cnt, double price): bookNo(s), units_sold(cnt), revenue(cnt * price) { } // remaining constructors all delegate to another constructor
Sales_data(): Sales_data('', , ) { }
Sales_data(string s): Sales_data(s, , ){ }
Sales_data(istream & is): Sales_data() { read(is, *this); }
};

Had the function bodies contain code, that code would be run before control return to the function body of the delegating constructor.

In practice, it is almost right to provide a default constructor if other constructor is defined.

class NoDefault{
public:
NoDefault( const string &);
// additional members follow, but no other constructor
}; Struct A{
NoDefault my_mem;
}; A a; // error: cannot synthesize a constructor of A Struct B{
B() { } // error: no initialization for b_member
Default b_member;
};

It is common mistake among programmers new to C++ to try to declare  an object initialized with the default constructor as function

    // opps! defines a function taking no parameter and returning an object of type Sales_data
Sales_data obj(); // define an object that use the default constructor
Sales_data obj;

Reference:

C++ Primer, Fifth Edition, chapter 7 Classes

随机推荐

  1. MySql is marked as crashed and should be repaired问题

    在一次电脑不知道为什么重启之后数据库某表出现了 is marked as crashed and should be repaired这个错误,百度了一下,很多都是去找什么工具然后输入命令之类的,因为 ...

  2. Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore[转]

    [转载]http://www.cnblogs.com/dolphin0520/p/3920397.html 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDow ...

  3. laravel-admin 创建数据库并生成控制器

    以user表为例 1. 生成迁移:php artisan make:migration create_users_table 在 database/migration 中生成迁移文件,可对迁移文件进行 ...

  4. Python3简单登录接口编写及遇到的问题分享

    1.程序目标 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 2.思路 利用python中的pickle模块,实现用户登录信息(采用dict)和被锁定用户信息(采用list)的存储.所以我预先 ...

  5. centos7环境下ELK部署之elasticsearch

    es部署:es只能用普通用户启动 博客园首发,转载请注明出处:https://www.cnblogs.com/tzxxh/p/9435318.html 一.环境准备: 安装jdk1.8.创建普通用户 ...

  6. ruby 批量下载王者荣耀皮肤

    主要采用ruby Parallel库提供的多线程方式: require 'unirest' require 'open-uri' require 'parallel' require 'json' u ...

  7. rtsp over tcp并设置多个options

    版权声明:本文为博主原创文章,未经博主允许不得转载. var vlc=document.getElementById("vlc"); var options = new Array ...

  8. sql 删除表字段中所有的空格

    源地址:http://bbs.csdn.net/topics/30322040 Sample表中有一个Name字段,目前当中有很多记录含有空格,如:“ 张 学 友 ”,如何用SQL语句将这些空格删掉, ...

  9. 厦门Uber优步司机奖励政策(12月21日-12.27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. springboot之RMI的使用

    1.RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的 ...