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. EntityFramework 7 Migrations 迁移命令
  2. 【转】Tomcat中server.xml配置图
  3. 总结一下这几天学习django的心得
  4. 双向BFS
  5. 【HDOJ】3727 Jewel
  6. [XML] ResourceManager一个操作Resource的帮助类 (转载)
  7. Crystal Report 在 VS 2010 中的使用和发布
  8. 容器 What, Why, How - 每天5分钟玩转容器技术(6)
  9. 看我如何从一个APK到最终拿下域管理权限
  10. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
  11. hdu1695 GCD(莫比乌斯入门题)
  12. calling c++ from golang with swig--windows dll (三)
  13. 微信域名检测的C#实现
  14. python的基本用法(三)字符串常用函数
  15. 浏览器与Tomcat交互
  16. HDU 2071 Max Num
  17. JPA+Hibernate 3.3 ——增删改查
  18. django 认证系统--1
  19. 四,ESP8266 TCP服务器(基于Lua脚本语言)
  20. POJ 3308 Paratroopers 最大流,乘积化和 难度:2

热门文章

  1. Spring Security学习笔记(三)
  2. mybatis调用存过程返回结果集和out参数值
  3. thinkphp3.2.3 HTML 页面跳转
  4. Python 爬虫 (二)
  5. C语言顺序表
  6. PTA基础编程题目集7-4 BCD解密
  7. React Router 4.0 实现路由守卫
  8. Codecraft-18 and Codeforces Round #458:D,Bash and a Tough Math Puzzle
  9. 北京Uber优步司机奖励政策(3月17日)
  10. 成都Uber优步司机奖励政策(3月11日)