C++重新解释_cast

发布于 2024-09-12 10:21:36 字数 310 浏览 6 评论 0原文

我不知道为什么这个简单的代码不起作用:

int main()
{
    const char* c = "ret";
    typedef unsigned char GOK_UINT8;
    typedef GOK_UINT8* pGOK_UINT8;
    const pGOK_UINT8  y = reinterpret_cast<const GOK_UINT8*>(c);
    
    return 0;
}

有人可以告诉我为什么 reinterpret_cast 不起作用吗?

I don't know why this simple code is not working:

int main()
{
    const char* c = "ret";
    typedef unsigned char GOK_UINT8;
    typedef GOK_UINT8* pGOK_UINT8;
    const pGOK_UINT8  y = reinterpret_cast<const GOK_UINT8*>(c);
    
    return 0;
}

Can someone tell me why the reinterpret_cast does not work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-09-19 10:21:36

有人可以告诉我为什么reinterpret_cast 不起作用吗?

AFAICS,reinterpret_cast 应该可以正常工作,但是之后的赋值应该会导致错误。

这是因为 const GOK_UINT8* 是一个指向 const GOK_UINT8 对象的非 const 指针,而 const pGOK_UINT8 是指向非 const 对象的 const 指针。
前者保护所引用的对象,后者保护引用该对象的指针。如果允许分配,您就可以更改 const GOK_UINT8* 旨在防止更改的对象。


请注意,typedefed 指针的行为很奇怪。这是因为(C 以及 C++ 中的 const 的声明语法很奇怪:A const 保护其左侧的内容,除非没有因此,在 T constT const* 中,类型为 T 受到保护,而在 T* const 中,指向 T 类型对象的指针受到保护。如果您有

typedef T* TPtr;

,则 TPtr const 再次使指针 const。 const TPtr 也是如此。 typedefed 指针要么指向 const 对象,要么指向非 const 对象,您无法更改它。您不能将 const 填充到 TPtr 附近并期望它能够保护指针引用的对象。

(顺便说一句,这就是为什么 STL 类必须同时定义迭代器和 const_iterator。)

Can someone tell me why the reinterpret_cast should not work?

AFAICS, the reinterpret_cast should work fine, but the assignment afterwards should cause an error.

That's because a const GOK_UINT8* is a non-const pointer to const GOK_UINT8 objects, while a const pGOK_UINT8 is a const pointer to non-const objects.
The former protects the object referred to, the latter the pointer referring to the object. If the assignment would be allowed, you could then change the object that the const GOK_UINT8* meant to protect from changing.


Note that typedefed pointers behave strange that way. That's because of the strange declaration syntax of const in (C and thus also in) C++: A const protects the thing to its left, unless there isn't anything, then it protects the thing to its right. So in T const and in T const*, the object of type T is protected, while in T* const the pointer to an object of type T is protected. If you have

typedef T* TPtr;

then TPtr const again makes the pointer const. So does const TPtr. A typedefed pointer either points to const or non-const objects, you can't change that. You can't stuff a const into the vicinity of TPtr and expect that to protect the objects the pointer refers to.

(BTW, this is why STL classes have to define both an iterator and a const_iterator.)

感情洁癖 2024-09-19 10:21:36

是的,sbi 是正确的。

相应地修改了您的代码,

const char* c = "ret";
typedef unsigned char GOK_UINT8;
typedef const GOK_UINT8* pGOK_UINT8;
pGOK_UINT8 y = reinterpret_cast<const GOK_UINT8*>(c);
printf("%s", y);

Yep sbi is correct.

Modified your code accordingly,

const char* c = "ret";
typedef unsigned char GOK_UINT8;
typedef const GOK_UINT8* pGOK_UINT8;
pGOK_UINT8 y = reinterpret_cast<const GOK_UINT8*>(c);
printf("%s", y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文