< C++ > initializer list 初始化列表(构造函数后面加个冒号的解释)

本文介绍 C++ 中如何使用构造函数初始化派生类和基类的成员变量。当创建派生类对象时,程序会先调用基类构造函数再调用派生类构造函数。可通过初始化列表来指定使用的基类构造函数。

Keypoint : 调用父类的构造函数(一般为有参构造函数),初始化类中的成员。


http://zhidao.baidu.com/link?url=N96OhNn11PxhkTNk7SCmd-2g3nrmKv-CtfjMKObGWo15eurHVS9Un03jwkP5BC5xErOXpTbgVHmEL3GBsV_8s_

http://zhidao.baidu.com/link?url=KkbSv_fAwmR-RVvp8KfZQyxyPaG4PlHKmLyTNpabEV82bX5Sw7OKqjf0LLvbXpDGN1mCuyHas-8APxMv5NJp4a

C++ primer 5th edition:

Remember
When creating an object of a derived class, a program first calls the base-class constructor and then
calls the derived-class constructor. The base-class constructor is responsible for initializing the inher-
ited data members. The derived-class constructor is responsible for initializing any added data mem-
bers. A derived-class constructor always calls a base-class constructor. You can use the initializer list
syntax to indicate which base-class constructor to use. Otherwise, the default base-class constructor
is used.


When an object of a derived class expires, the program first calls the derived-class destructor and
then calls the base-class destructor.


Member Initializer Lists


A constructor for a derived class can use the initializer list mechanism to pass values along to a base-
class constructor. Consider this example:


derived::derived(type1 x, type2 y) : base(x,y) // initializer list
{
...
}


Here, derived is the derived class, base is the base class, and x and y are variables used by the base-
class constructor. If, say, the derived-class constructor receives the arguments 10 and 12, this mecha-
nism then passes 10 and 12 on to the base-class constructor defined as taking arguments of these
types. Except for the case of virtual base classes (see Chapter 14, “Reusing Code in C++”), a class
can pass values back only to its immediate base class. However, that class can use the same mecha-
nism to pass back information to its immediate base class, and so on. If you don’t supply a base-class
constructor in a member initializer list, the program uses the default base-class constructor. The
member initializer list can be used only with constructors.

### C++ 构造函数中的列表初始化语法和用法 在现代 C++ 中,构造函数列表初始化是一种强大的工具,用于显式地初始化类成员变量。这种技术不仅提高了代码的安全性和可读性,还减少了未初始化变量的风险[^1]。 #### 列表初始化的基本概念 通过使用冒号 `:` 和大括号 `{}` 的组合,可以在构造函数中指定成员变量的初始值。这种方式被称为 **成员初始化列表** (Member Initialization List),它允许开发者精确控制对象创建时的行为。以下是几个关键点: - 成员初始化列表优先于构造函数体内的赋值操作。 - 对于某些数据类型(如常量或引用),必须通过成员初始化列表来设置其初始值。 - 使用默认值声明可以减少重复代码并增强灵活性[^2]。 #### 示例代码展示 下面是一个完整的例子,展示了如何利用成员初始化列表以及默认值声明实现高效的类设计: ```cpp class Example { public: // 默认构造函数 Example() : value_(0), name_("Default") {} // 参数化构造函数 Example(int val, const std::string& str) : value_(val), name_(str), pi_(3.14f) {} private: int value_; // 不需要额外初始化 float pi_ = 3.14159f; // 提供了默认值 std::string name_; // 需要显式初始化 }; ``` 上述代码片段说明了以下几点: - 变量 `value_` 被明确地初始化为零。 - 字符串型成员变量 `name_` 接受了一个字符串参数作为输入。 - 浮点数 `pi_` 即使没有出现在任何构造函数里也会被赋予预定义数值。 此外,在更复杂的场景下还可以结合标准库容器一起工作: ```cpp #include <vector> #include <map> class ComplexExample { public: ComplexExample(std::initializer_list<int> ilist) : numbers(ilist), mapping({{1,"One"}, {2,"Two"}}){} private: std::vector<int> numbers; std::map<int,std::string> mapping; }; ``` 这里演示了怎样借助 `std::initializer_list` 来简化向量和其他集合类型的填充过程。 #### 性能与兼容性的考量 值得注意的是,尽管引入了许多新特性以改善编程体验,但在跨平台开发过程中仍需注意不同编译器的支持情况。例如,“long long”整数类型虽然已被广泛接受,但仍可能存在个别例外情形[^3]。 --- 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值