为什么C+&#x2B中的const转换操作员毫无意义?
在“ C ++底漆”,练习14.47中,有一个问题:
解释这两个转换之间的区别 操作员:
struct intemal { 操作员const int(); 操作员int()const; }
我不知道为什么我在github上找到的答案说第一个const
是毫无意义的,因为对于一个转换操作员不应定义返回类型,此const
这是未指定的,编译器将忽略它。但是我还发现一些人说这意味着该函数将返回const
值。
所以,我想知道哪一个是正确的,为什么?
In "C++ Primer", exercise 14.47, there is a question:
Explain the difference between these two conversion
operators:struct Integral { operator const int(); operator int() const; }
I don't know why the the answer I found on GitHub says that the first const
is meaningless, because for one conversion operator should not define return type, this const
here is unspecified, it will be ignored by the compiler. But I also found some guys say that it means the function will return a const
value.
So, I wonder which one is correct, and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 expr#6
这意味着在您的特定示例中,
const int
将在进一步之前调整为int
分析由于int
是内置类型而不是类型。即使第一个转换功能的返回类型为
const int
,在进行进一步分析之前,它将调整为int
。而第二个转换功能上的
const
表示此
指针该函数类型const intempal*
。这意味着它(转换函数)可以与const
以及非constIntegral
对象一起使用。这是从 class.this.thisThis is because of expr#6 which states:
This means that in your particular example,
const int
will be adjusted toint
before further analysis sinceint
is a built in type and not a class type.Even though the return type of the first conversion function is
const int
, it will be adjusted toint
prior to any further analysis.While the
const
on the second conversion function means that thethis
pointer inside that function is of typeconst Integral*
. This means that it(the conversion function) can be used withconst
as well as non-constIntegral
object. This is from class.this