char[N] 与 char *
谁能告诉我为什么下面的代码会出现编译错误?
struct Foo {
char m_p[8];
inline operator char *() const { return m_p; }
};
在 GCC 4.5 中,我收到以下消息:
错误:“const”的转换无效 字符*'到'字符*'
而 Digital Mars 编译器对此没有问题。
编辑:下面列出的答案提到了函数的 const 限定符。我真正不明白的是为什么下面的代码没有这样的问题:
struct Foo2 {
char *m_p;
inline operator char *() const { return m_p; }
};
Could anyone tell me why there is a compile error with the following code?
struct Foo {
char m_p[8];
inline operator char *() const { return m_p; }
};
With GCC 4.5 I am given the message:
error: invalid conversion from 'const
char*' to 'char*'
while the Digital Mars compiler has no problem with it.
Edit: The answers listed below mention the const qualifier on the function. What I really don't get then is why the following code has no such problem:
struct Foo2 {
char *m_p;
inline operator char *() const { return m_p; }
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于该运算符是 const 函数,因此
this
及其所有成员 (m_p
) 都应该是 const。所以m_p
的类型是const char[8]
。它不能隐式转换为char*
,因为const
性质将会丢失。转换为const char*
就可以了。您可以让它返回 const char*
或删除 const 限定符
或提供这两种方法。
数字火星在这里是错误的。或者也许您需要启用一些警告。
Since that operator is a
const
function,this
and all its members (m_p
) is supposed to be const. So the type ofm_p
isconst char[8]
. It cannot be implicitly converted tochar*
as theconst
-ness will be lost. Conversion toconst char*
is OK.You could either make it return a
const char*
or remove the
const
qualifieror provide both methods.
Digital Mars is wrong here. Or maybe you need to enable some warnings.
因为在函数内(const 限定)数组的类型是 const char[8],并且无法转换为
char*
,只能转换为>const char*
。要么放弃函数上的 const 限定,要么将其添加到返回类型中。另外,内联在这里是多余的,因为类定义中定义的成员无论如何都是隐式内联的。
Because within the function (being const-qualified) the type of your array is
const char[8]
, and that can't be converted tochar*
, onlyconst char*
.Either ditch the const-qualification on the function, or add it to the return type. Also,
inline
is redundan here, because members defined within the class definition are implicitlyinline
anyway.char m_p[8] 和 char *m_p 之间的区别在于,前者是对象的一部分,当从 const 限定方法访问时,它将变成 const。对于指针,它指向其他地方,您可以从 const 限定方法返回它,而无需强制它为 const。
The difference between char m_p[8] and char *m_p is that the former is a part of your object and when accessed from const qualified method it will become const. For the pointer it points somewhere else and you can return it from const qualified method without forcing it to be const.
您的运算符被标记为 const,因此它获得一个 const this 指针。这意味着您尝试通过
this
指针访问的struct
的任何字段实际上都是const
(因为它是通过const
-path),因此当您执行return m_p;
时,m_p
会衰减为const char *
,其中turn 无法转换为char *
(运算符的返回类型),因为删除const
修饰符需要const_cast
。长话短说:如果您希望该运算符为 const,则返回类型必须为 const char *,否则您会遇到 const 的情况 方法(=>可以在
const
对象上调用)能够让调用者修改该对象(通过返回的非const
指针) )即使这应该被const
禁止。Your operator is marked as
const
, so it gets aconst
this
pointer. This means that whatever field of thestruct
you'll try to access via thatthis
pointer will actually beconst
(because it's being accessed through aconst
-path), so when you doreturn m_p;
m_p
decays to aconst char *
, which in turn cannot be converted tochar *
(the return type of the operator), because dropping theconst
modifier requires aconst_cast
.Long story short: if you want that operator to be
const
, then the return type must beconst char *
, otherwise you'd have a situation where aconst
method (=>which can be called on aconst
object) would be able to let the caller modify the object (via the non-const
pointer returned) even if this should be forbidden by theconst
.