gpt4 book ai didi

c++ - 如何将 const unsigned short 转换为 unsigned short?

转载 作者:太空狗 更新时间:2023-10-29 21:44:17 28 4
gpt4 key购买 nike

到目前为止我已经试过了,但我仍然在下面的代码中遇到错误:

#include<iostream>
typedef unsigned short unichar;
typedef const unichar unimap_t[2];
unimap_t x = {0x0004,0x00ff};

const unimap_t * ret()
{

return x;
}
int main()
{
unsigned short* pX2 = const_cast < unsigned short* > (ret());
std::cout <<pX2[1];
return 0;
}

我收到以下错误。

a.cpp: In function ‘int main()’:
sa.cpp:22:60: error: invalid const_cast from type ‘const unichar (*)[2]
{aka const short unsigned int (*)[2]}’ to type ‘short unsigned int*’`

最佳答案

首先,您返回的不是 unsigned short*,而是一个unsigned short (*)[2],指向2个unsigned数组的指针
。这可能不是您想要的;的签名你的功能可能应该是:

unichar const* ret();

(C 风格的数组从根本上被破坏了,代表类型系统中的特例。)或者,您可以想要返回引用:

unimap_t const& ret();

这应该转换为 unsigned short const*

关于c++ - 如何将 const unsigned short 转换为 unsigned short?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562974/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com