更新时间:2022-12-28 11:00:29
此语句:
Foo(x);
不是函数调用,也不是构造函数调用。这只是一个声明,说 x
是 Foo
类型。括号在声明符 x
周围是可选的,因此等效于:
is not a function call, or a constructor call. It's just a declaration, that says x
is of type Foo
. The parentheses are optional around the declarator x
, so it's equivalent to:
Foo x;
这当然会产生错误,因为您已经有 std :: string
名为 x
,并且您不能为同一范围内的多个实体提供相同的名称。
This of course gives an error, since you already have a std::string
named x
, and you can't give the same name to multiple entities in the same scope.
请注意表达式:
Foo(x)
与上面的声明(带有;
)不同。根据使用的上下文,该表达式可能表示不同的含义。
is different than the statement above (with the ;
). This expression can mean different things depending on the context it is used in.
例如,这段代码:
Foo foo = Foo(x);
非常好。这确实从表达式 Foo(x)
复制一个名为 foo
的变量的初始化,这是一个临时的 Foo
由参数 x
构造。 (这里不是特别重要,但是从c ++ 17开始,在右侧没有临时对象;该对象只是在适当的位置构造)。
is perfectly fine. This does copy initialization of a variable named foo
, from the expression Foo(x)
, which is a temporary Foo
constructed from the argument x
. (It's not particularly important here, but from c++17, there's no temporary on the right hand side; the object just gets constructed in place).