简体   繁体   English

C ++中的构造函数调用或函数样式转换

[英]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.

相关问题 在 C++ 中使用函数式类型转换初始化变量 - Initializing a variable with function-style cast in C++ 函数式强制转换与构造函数 - Function-style cast vs. constructor C ++ Xcode期望'('用于函数样式的强制转换或类型构造 - c++ Xcode expected '(' for function-style cast or type construction C++:“预期的 '(' 用于函数式强制转换或类型构造”错误 - C++: “Expected '(' for function-style cast or type construction” Error C风格的演员是否与功能风格的演员阵容相同? - Is a C-style cast identical to a function-style cast? 转换需要reinterpret_cast,C样式转换或函数样式转换 - Conversion requires reinterpret_cast, C-style cast or function-style cast C ++函数式转换的目的究竟是什么? - What exactly is or was the purpose of C++ function-style casts? 函数式强制转换或类型构造需要 '(' - Expected '(' for function-style cast or type construction 转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换) - Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) 转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换)枚举 - Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) enum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM