[英]Conversion from const char[] to class with constructor from string
I try to pass a string to a function instead an object of class A
, but I get an error.我尝试将字符串传递给 function 而不是 class A
的 object ,但出现错误。 Why is it not working and how can I make it work?为什么它不工作,我怎样才能让它工作?
#include <string>
class A {
public:
A(const std::string &str) {
}
};
void test(const A &a) {
}
int main()
{
A a("abc"); //it's ok
test(a); //it's ok
test("abc"); //error?
return 0;
}
I don't want to pass a string object to test
like test(std::string("abc"))
.我不想传递一个字符串 object 来test
test(std::string("abc"))
。 I need convert it from const char[]我需要从 const char[] 转换它
In the implicit conversion sequence of one type to another, there is a rule that states: At most, only one user defined conversion can be applied.在一种类型到另一种类型的 隐式转换序列中,有一条规则规定:最多只能应用一个用户定义的转换。 In在
test("abc");
You need two user defined conversions.您需要两个用户定义的转换。 First you need to conversion from const char[N]
to std::string
, which is considered a user defined conversion 1 .首先,您需要从const char[N]
转换为std::string
,这被认为是用户定义的转换1 。 Then you need to convert that std::string
to a A
, which would be a second user defined conversion.然后您需要将该std::string
转换为A
,这将是第二个用户定义的转换。 That is the reason you get the error这就是您收到错误的原因
To fix, you can use要修复,您可以使用
test(A("abc"));
//or
test(std::string("abc"));
and now you have one explicit user defined conversion, and one implicit conversion, satisfying the one implicit conversion rule.现在您有一个显式的用户定义转换和一个隐式转换,满足一个隐式转换规则。
You could also provide a constructor for A
that accepts a const char*
so that you can directly convert from "abc"
to an A
.您还可以为A
提供一个接受const char*
的构造函数,以便您可以直接从"abc"
转换为A
。
1: Even though std::string
is part of the standard library, it is not a "built in" type, but a library type. 1:尽管std::string
是标准库的一部分,但它不是“内置”类型,而是库类型。 This means it is counted as a user defined type, even though it shipped with your implementation.这意味着它被视为用户定义的类型,即使它随您的实现一起提供。
The problem is that the standard only allow direct implicit conversions.问题是标准只允许直接隐式转换。
The following conversions are defined: const char*
-> std::string
and const std::string
-> A
定义了以下转换: const char*
-> std::string
和const std::string
-> A
That means that you can safely pass a const std::string
where a A
is expected, but not an implicitly convertible to std::string
object.这意味着您可以安全地将const std::string
传递到预期A
的位置,但不能隐式转换为std::string
object。 Transitivity does not work here.传递性在这里不起作用。
So you will have to do:所以你必须这样做:
test(std::string("abc")); // ok...
The problem is with the conversions.问题在于转换。 "There can be only one" user defined conversion. “只能有一个”用户定义的转换。
Here is:这是:
const char * -> std::string -> A
So you need to remove a conversion.所以你需要删除一个转换。 A way to fix it is to add the suffix 's' at the the end of the string:解决它的一种方法是在字符串末尾添加后缀“s”:
test("ABC"s);
https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s
Another way is to add the function:另一种方法是添加function:
void test(const std::string& s)
{
test(A(s)) ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.