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. C++读取字符串数据的两种方式

    C++读取字符串数据的两种方式 对于同样的样例输入: ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Ride ...

  2. jquery实现表单验证简单实例

    /* 描述:基于jquery的表单验证插件. */ (function ($) { $.fn.checkForm = function (options) { var root = this; //将 ...

  3. Java : Netty 入门案例

    接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...

  4. [Doctrine Migrations] 数据库迁移组件的深入解析二:自定义集成

    自定义命令脚本 目录结构 目前的项目结构是这样的(参照代码库): 其中,db/migrations文件夹是迁移类文件夹,config/db.php是我们项目原有的db配置,migrations.php ...

  5. hadoop生态搭建(3节点)-13.mongodb配置

    # 13.mongodb配置_副本集_认证授权# ==================================================================安装 mongod ...

  6. Hadoop原理之——HDFS原理

    Hadoop 3个核心组件: 分布式文件系统:Hdfs——实现将文件分布式存储在很多的服务器上(hdfs是一个基于Linux本地文件系统上的文件系统) 分布式运算编程框架:Mapreduce——实现在 ...

  7. MapReduce序列化及分区的java代码示例

    概述 序列化(Serialization)是指把结构化对象转化为字节流. 反序列化(Deserialization)是序列化的逆过程.把字节流转为结构化对象. 当要在进程间传递对象或持久化对象的时候, ...

  8. rails应用使用carrierwave和mini_magick上传用户头像

    1. 在Gemfile添加 gem 'carrierwave' gem 'mini_magick' 执行 bundle install 2. 生成uploader rails generate upl ...

  9. 反射vs简单工厂模式

    interface Computer { void printpc(); } class lenovo implements Computer { @Override public void prin ...

  10. linux 搭建ss

    因为收藏的各种教程被xx,所以决定自己写 第一步.安装ss sudo pip install shadowsocks 第二步.配置IP.端口.密码.加密方式 vi /etc/shadowsocks.j ...