Initialization and constructor and conversion
搞清楚initialization 和 constructor 之间的对应关系非常重要,下面是比较好的素材。
以initialization为切入点
"Conversion is defined in terms of initialization" 并且 "各种initialization可能考虑多种constructor,即一对多",因此以initialization为切入点是比较好的。
Conversion and initialization
Conversion is defined in terms of initialization
implicit conversion
cppreference Copy initialization
Implicit conversion is defined in terms of copy-initialization
implicit conversion是基于copy-initialization而定义的,copy-initialization是不考虑 explicit constructors 的,因此 Implicit conversion 是不考虑 explicit constructors 。因此,如果一个class只有 explicit constructors ,因此,它就无法进行implicit conversion。
cppreference Converting constructor
It is said that a converting constructor specifies an implicit conversion from the types of its arguments (if any) to the type of its class. Note that non-explicit user-defined conversion function also specifies an implicit conversion.
explicit conversion
cppreference Explicit type conversion
If there are more than one expression or braced-init-list (since C++11) in parentheses, new_type must be a class with a suitably declared constructor. This expression is a prvalue of type new_type designating a temporary (until C++17)whose result object is (since C++17) direct-initialized with expression-list.
A single-word type name followed by a braced-init-list is a prvalue of the specified type designating a temporary (until C++17)whose result object is (since C++17) direct-list-initialized with the specified braced-init-list. If new_type is (possibly cv-qualified)
void
, the expression is a void prvalue without a result object (since C++17).
explicit conversion是否会考虑 Copy initialization ?我觉得它应该不会的。
Initialization and constructor
1、C++定义了多种initialization,各种initialization可能考虑多种constructor,即一对多