作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定以下两个构造函数签名,是否可以使用 Couple("George", "Nora")
构造一个 Couple
?我的编译器提示如下所示的错误。如果我用 Couple(std::string("George"), std::string("Nora"))
调用它,它编译正常。我猜隐式转换存在问题,这让我感到惊讶,因为我认为将 char* 转换为字符串会很好。
class Person
{
public:
Person(const std::string& name);
};
class Couple
{
public:
Coordinate(const Person& p1, const Person& p2, const Optional<Person>& = Optional<Person>());
};
TestCouple.cpp:69: error: no matching function for call to `Couple::Couple(const char[7], const char[5])'
TestCouple.h:24: note: candidates are: Couple::Couple(const Person&, const Person&, const Optional<fox::Person>&)
最佳答案
确实,一个转换序列不能包含多个隐式用户定义转换;该标准在 C++11 12.3/4 中指定了这一点:
At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.
在你的情况下,需要两个(char const[]
到 std::string
到 Person
),因此隐式转换不可能。
关于c++ - 在 C++ 中构造对象时进行两次隐式转换是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11188826/
我是一名优秀的程序员,十分优秀!