隐式转换
在下面的例子中,作者对隐式转换做了几点注释。您能否更详细地解释一下这些评论,我对这些评论不太清楚。谢谢。
class String{
explicit String(int n);
String(const char *p);
}
String s1= ‘a’; //error: no implicit char->String conversion
void f(String);
String g( )
{
f(10); // error: no implicit int->String conversion
return 10; // error: no implicit int-> String conversion
}
In the following example, the author made several comments on the implicit conversion. Can you explain more detail on these comments, which are not very clear to me. Thanks.
class String{
explicit String(int n);
String(const char *p);
}
String s1= ‘a’; //error: no implicit char->String conversion
void f(String);
String g( )
{
f(10); // error: no implicit int->String conversion
return 10; // error: no implicit int-> String conversion
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
String
类有两个构造函数;一种用于从int
构造String
,另一种用于从指向char
的 const 指针构造String
。因此,这两个构造函数也是转换函数,因为它们实际上将一种类型转换为另一种类型。然而,第一个构造函数是一个显式构造函数。虽然第二个构造函数允许从指针到char
到String
的隐式转换,但第一个构造函数要求您显式请求转换。例如:
第一个错误注释只是说没有用于将
char
转换为String
的构造函数。同样,我们只有两个构造函数:一个用于转换int
,另一个用于转换char
的 const 指针。第二个错误涉及将
int
作为参数传递给需要String
的函数。这意味着该函数必须隐式从int
构造一个String
。这是无法完成的,因为相关的构造函数是显式的。如果您从int
构造一个String
,然后将该String
传递给函数,一切都会很好。第三个错误与第二个错误完全相同,只是这里隐式转换(失败)是在返回值应为
String
时返回int
。需要注意的一件有趣的事情是,如果代码中的整数为 0 而不是 10,则代码将编译。原因是 0 可以隐式转换为地址(NULL 地址),并且对于采用指针的构造函数来说,这是一个有效值。
The
String
class has two constructors; one for constructing aString
from anint
and one for constructing aString
from a const pointer to achar
. These two constructors are hence also conversion functions, because they really convert one type into another. The first constructor, however, is an explicit constructor. While the second constructor allows for implicit conversion from a pointer-to-char
to aString
, the first constructor requires you to ask for the conversion explicitly.For example:
The first error comment simply says that there is no constructor for converting a
char
to aString
. Again, we have only two constructors: one for converting anint
, the other a const pointer tochar
.The second error talks about passing as parameter an
int
to a function that requires aString
. This implies that the function must construct aString
from anint
implicitly. This can't be done because the relevant constructor is explicit. If you'd construct aString
from anint
and then pass thatString
to the function all would be well.The third error is exactly the same as the second, only here the implicit conversion (which fails) is at returning an
int
when the return value should be aString
.One interesting thing to note is that the code would compile if the integer in your code would be 0 and not 10. The reason is that 0 can be implicitly casted to an address (the NULL address), and that is a valid value for the constructor that takes a pointer.
作者正在记录一些情况,在这些情况下,编译器会因为没有转换或选择的转换被标记为“显式”而给出错误。如果有一个实际可行的案例,代码可能会更清晰:
The author is documenting cases in which the compiler will give you an error because either there is no conversion, or the conversion selected is marked
explicit
. The code might be clearer with a case that would actually work:这些注释基本上表明编译器无法知道如何与
String
数据类型进行转换。请参阅:http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr383.htm
另外: http://www.glenmccl.com/tip_023.htm
The comments basically say that the compiler has no way of knowing how to convert to/from the
String
datatype.See: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr383.htm
Also: http://www.glenmccl.com/tip_023.htm
作为附加信息。首先,为什么我们需要担心隐式转换?考虑以下场景。
它仍然编译,并将整数“2”转换为测试对象,这不是我的意图,而是比较 t1 和 t2。在单参数构造函数前面使用“显式”关键字可以避免这种静默转换。希望这有帮助!
As addition info. first of all why we need to bother about the implicit conversion? Consider the following scenario.
It still compiled, and converted the integer '2' into Test Object, which was not my intention, but to compare t1 and t2. Using 'explicit' keyword infront of the single argument constructor, avoids such silent conversion. Hope this helps!!!..