[C++] Class (part 2)

时间:2023-03-09 17:49:59
[C++] Class (part 2)

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. GoogleHacking相关技巧

    转自https://www.cnblogs.com/anka9080/p/googlehack.html 0x 01 GoogleHack语法 Site 指定域名 Intext 正文中出现关键字的网页 ...

  2. Python学习笔记:第3天 字符串的操作

    目录 1. python的数据类型 2. int类型的操作 3. bool类型 4. 字符串的操作 5. for循环 1. python的数据类型 int 整数 str 字符串.一般不会用字符串保存大 ...

  3. Java学习笔记十七:Java中static使用方法

    Java中static使用方法 一:Java中的static使用之静态变量: 我们都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的 ...

  4. ggplot2画简单的heatmap

    gg_heatmap gg_heatmap PeRl ggplot2 heatmap 偶然的机会,发现ggplot2画的heatmap也挺好看的,除了不能画出聚类树来(手动滑稽). 随意新建了两个矩阵 ...

  5. 15-oauth2+oidc实现Server部分

    1-我们使用之前项目的mvcCookieAuthSampe2进行改造 1.1  增加IdentityServer4 2-增加Config.cs文件,对IdentityServer提供相关的配置数据 u ...

  6. Git Status 中文乱码解决

    现象: jb@H39:~/doc$ git statusOn branch masterYour branch is up-to-date with 'origin/master'. Untracke ...

  7. window下创建虚拟环境

    一. windows下创建虚拟环境 1. 终端下执行命令:python -m pip install -upgrade pip 2. pip install virtualenv 3. 在本地创建一个 ...

  8. 北京Uber优步司机奖励政策(1月15日)

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

  9. 北京优步UBER司机B组最新奖励政策、高峰翻倍奖励、行程奖励、金牌司机奖励【每周更新】

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

  10. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 – 员工离职管理

    C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 – 员工离职管理 当公司有几万人,上千家加盟网点,几个庞大的直属分公司后,系统账户的有效管理也是一个头疼的问题,把所有的帐户及时进行科学 ...