我了解到,在C++中,功能样式转换与构造
typedef foo* mytype;
(mytype) a // C-style cast
和
mytype(a) // function-style cast
做同样的事情。
但我注意到函数式转换与构造函数共享相同的语法。 是不是有不明确的情况下,我们不知道它是一个演员或建设者?
char s [] = "Hello";
std::string s2 = std::string(s); // here it's a constructor but why wouldn't it be ...
std::string s3 = (std::string) s; // ... interpreted as a function-style cast?
实际上,在您显示的示例中*两个*个案都会调用一个'std :: string'构造函数。 *相同*构造函数。 –
注意:在C++中,你通常应该更喜欢明确的转换static_cast,reinterpret_cast,const_cast,dynamic_cast,static_pointer_cast,reinterpret_pointer_cast,const_pointer_cast和dynamic_pointer_cast(以及move& '前锋' - 是的,他们*只是在C风格演员阵容*上)。他们更容易在代码中搜索,对于意图和安全性更加明确(例如,最糟糕的C风格演员阵容是生成'reinterpret_cast',后面跟着'const_cast')。 –