[英]constructor call or function-style cast in C++
If I have the following c++ code: 如果我有以下c ++代码:
class foo{
public:
explicit foo(int i){};
};
void f(const foo &o){
}
And then I call 然后我打电话
f(foo(1));
Is foo(1)
constructor call or function-style cast? 是
foo(1)
构造函数调用还是函数式转换?
他们是一样的东西。
它是一个函数式转换,导致构造函数调用,所以两者都是。
5.2.3 Explicit type conversion (functional notation)
5.2.3显式类型转换(功能表示法)
1 A simple-type-specifier (7.1.6.2) or typename-specifier (14.6) followed by a parenthesized expression-list constructs a value of the specified type given the expression list.
1 简单类型说明符 (7.1.6.2)或类型名称说明 符 (14.6)后跟带括号的表达式列表 ,在给定表达式列表的情况下构造指定类型的值。 If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4).
如果表达式列表是单个表达式,则类型转换表达式与相应的强制转换表达式 (5.4) 等效(在定义中,如果在含义中定义 )。 ...
...
Your code creates a temporary, using the constructor you have with the argument's value 1, and binds it to a const reference. 您的代码使用您具有参数值1的构造函数创建一个临时代码,并将其绑定到const引用。 The temporary's lifetime ends at the end of the statement where it was created.
临时生命周期在创建它的语句结束时结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.