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. js内部事件机制--单线程原理

    原文地址:https://www.xingkongbj.com/blog/js/event-loop.html http://www.haorooms.com/post/js_xiancheng ht ...

  2. ACM 2000~2002

    ACM  2000  输入三个字符后,按各个字符的ASCⅡ码从小打到的顺序输出这三个字符. import java.util.Scanner; public class Lengxc {public ...

  3. 怎么将oracle的sql文件转换成mysql的sql文件

    怎么将sql文件导入PowerDesigner中的方法(将oracle的sql文件转换成mysql的sql文件)呢? 怎么将xx.sql文件的数据库结构导入powerdesigner 的方法呢? 现讲 ...

  4. JS 原型总结

    参考: (从内存角度)简单类型与复杂类型及原型链

  5. js 中~~是什么意思?

    其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1~~false == 0~~"" == 0~~[] == 0 ~~undefined ==0~~!undef ...

  6. python中的super怎么用?

    面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...

  7. 分析nginx 日志常用命令

    一.概念 并发连接数    客户端向服务器发起请求,并建立了TCP连接.每秒钟服务器链接的总TCP数量,就是并发连接数.请求数    请求数指的是客户端在建立完连接后,向http服务发出GET/POS ...

  8. Hive(6)-DML数据操作

    一. 数据导入 1. 语法 load data [local] inpath 'path' [overwrite] into table table_name [partition (partcol1 ...

  9. STM32(10)——窗口看门狗

    简介: 窗口看门狗(WWDG)通常被用来监测由外部干扰或不可预见的逻辑条件造成的应用程序背离正常的运行序列而产生的软件故障.除非递减计数器的值在 T6 位 (WWDG->CR 的第六位)变成 0 ...

  10. 使用ntp协议同步本地时间(C语言)

    使用ntp协议同步本地时间 同步服务器使用的东北大学网络授时服务:ntp.neu.edu.cn更多ntp服务器 http://www.ntp.org.cn/ 源代码来自网络,经本人精简ntp部分,供大 ...