gpt4 book ai didi

c++ - 在 C++ 中构造对象时进行两次隐式转换是否有效?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:46 27 4
gpt4 key购买 nike

给定以下两个构造函数签名,是否可以使用 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::stringPerson),因此隐式转换不可能。

关于c++ - 在 C++ 中构造对象时进行两次隐式转换是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11188826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com