mirror of
https://github.com/CnTransGroup/EffectiveModernCppChinese.git
synced 2025-02-05 00:30:31 +08:00
Update item7.md
This commit is contained in:
parent
caad38385d
commit
f25c878636
@ -44,7 +44,7 @@ std::vector<int> v{ 1, 3, 5 }; //v初始内容为1,3,5
|
|||||||
括号初始化也能被用于为非静态数据成员指定默认初始值。C++11允许"="初始化不加花括号也拥有这种能力:
|
括号初始化也能被用于为非静态数据成员指定默认初始值。C++11允许"="初始化不加花括号也拥有这种能力:
|
||||||
````cpp
|
````cpp
|
||||||
class Widget{
|
class Widget{
|
||||||
...
|
…
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int x{ 0 }; //没问题,x初始值为0
|
int x{ 0 }; //没问题,x初始值为0
|
||||||
@ -95,7 +95,7 @@ class Widget {
|
|||||||
public:
|
public:
|
||||||
Widget(int i, bool b); //构造函数未声明
|
Widget(int i, bool b); //构造函数未声明
|
||||||
Widget(int i, double d); //std::initializer_list形参
|
Widget(int i, double d); //std::initializer_list形参
|
||||||
...
|
…
|
||||||
};
|
};
|
||||||
Widget w1(10, true); //调用第一个构造函数
|
Widget w1(10, true); //调用第一个构造函数
|
||||||
Widget w2{10, true}; //也调用第一个构造函数
|
Widget w2{10, true}; //也调用第一个构造函数
|
||||||
@ -109,7 +109,7 @@ public:
|
|||||||
Widget(int i, bool b); //同上
|
Widget(int i, bool b); //同上
|
||||||
Widget(int i, double d); //同上
|
Widget(int i, double d); //同上
|
||||||
Widget(std::initializer_list<long double> il); //新添加的
|
Widget(std::initializer_list<long double> il); //新添加的
|
||||||
...
|
…
|
||||||
};
|
};
|
||||||
````
|
````
|
||||||
`w2`和`w4`将会使用新添加的构造函数构造,即使另一个非`std::initializer_list`构造函数对于实参是更好的选择:
|
`w2`和`w4`将会使用新添加的构造函数构造,即使另一个非`std::initializer_list`构造函数对于实参是更好的选择:
|
||||||
@ -137,7 +137,7 @@ public:
|
|||||||
Widget(int i, double d); //同之前一样
|
Widget(int i, double d); //同之前一样
|
||||||
Widget(std::initializer_list<long double> il); //同之前一样
|
Widget(std::initializer_list<long double> il); //同之前一样
|
||||||
operator float() const; //转换为float
|
operator float() const; //转换为float
|
||||||
...
|
…
|
||||||
};
|
};
|
||||||
|
|
||||||
Widget w5(w4); //使用小括号,调用拷贝构造函数
|
Widget w5(w4); //使用小括号,调用拷贝构造函数
|
||||||
@ -157,7 +157,7 @@ public:
|
|||||||
Widget(int i, bool b); //同之前一样
|
Widget(int i, bool b); //同之前一样
|
||||||
Widget(int i, double d); //同之前一样
|
Widget(int i, double d); //同之前一样
|
||||||
Widget(std::initializer_list<bool> il); //现在元素类型为bool
|
Widget(std::initializer_list<bool> il); //现在元素类型为bool
|
||||||
... //没有隐式转换函数
|
… //没有隐式转换函数
|
||||||
};
|
};
|
||||||
|
|
||||||
Widget w{10, 5.0}; //错误!要求变窄转换
|
Widget w{10, 5.0}; //错误!要求变窄转换
|
||||||
@ -173,7 +173,7 @@ public:
|
|||||||
Widget(int i, double d); //同之前一样
|
Widget(int i, double d); //同之前一样
|
||||||
//现在std::initializer_list元素类型为std::string
|
//现在std::initializer_list元素类型为std::string
|
||||||
Widget(std::initializer_list<std::string> il);
|
Widget(std::initializer_list<std::string> il);
|
||||||
... //没有隐式转换函数
|
… //没有隐式转换函数
|
||||||
};
|
};
|
||||||
|
|
||||||
Widget w1(10, true); // 使用小括号初始化,调用第一个构造函数
|
Widget w1(10, true); // 使用小括号初始化,调用第一个构造函数
|
||||||
@ -190,7 +190,7 @@ public:
|
|||||||
Widget(); //默认构造函数
|
Widget(); //默认构造函数
|
||||||
Widget(std::initializer_list<int> il); //std::initializer_list构造函数
|
Widget(std::initializer_list<int> il); //std::initializer_list构造函数
|
||||||
|
|
||||||
... //没有隐式转换函数
|
… //没有隐式转换函数
|
||||||
};
|
};
|
||||||
|
|
||||||
Widget w1; //调用默认构造函数
|
Widget w1; //调用默认构造函数
|
||||||
@ -226,7 +226,7 @@ template<typename T, //要创建的对象类型
|
|||||||
void doSomeWork(Ts&&... params)
|
void doSomeWork(Ts&&... params)
|
||||||
{
|
{
|
||||||
create local T object from params...
|
create local T object from params...
|
||||||
...
|
…
|
||||||
}
|
}
|
||||||
````
|
````
|
||||||
在现实中我们有两种方式实现这个伪代码(关于`std::forward`请参见[Item25](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/5.RRefMovSemPerfForw/item25.md)):
|
在现实中我们有两种方式实现这个伪代码(关于`std::forward`请参见[Item25](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/5.RRefMovSemPerfForw/item25.md)):
|
||||||
@ -237,7 +237,7 @@ T localObject{std::forward<Ts>(params)...}; //使用花括号
|
|||||||
考虑这样的调用代码:
|
考虑这样的调用代码:
|
||||||
````cpp
|
````cpp
|
||||||
std::vector<int> v;
|
std::vector<int> v;
|
||||||
...
|
…
|
||||||
doSomeWork<std::vector<int>>(10, 20);
|
doSomeWork<std::vector<int>>(10, 20);
|
||||||
````
|
````
|
||||||
如果`doSomeWork`创建`localObject`时使用的是小括号,`std::vector`就会包含10个元素。如果`doSomeWork`创建`localObject`时使用的是花括号,`std::vector`就会包含2个元素。哪个是正确的?`doSomeWork`的作者不知道,只有调用者知道。
|
如果`doSomeWork`创建`localObject`时使用的是小括号,`std::vector`就会包含10个元素。如果`doSomeWork`创建`localObject`时使用的是花括号,`std::vector`就会包含2个元素。哪个是正确的?`doSomeWork`的作者不知道,只有调用者知道。
|
||||||
|
Loading…
Reference in New Issue
Block a user