【问题标题】:C/C++ typecasting convert int** to const int** [duplicate]C/C++ 类型转换将 int** 转换为 const int** [重复]
【发布时间】:2016-01-27 06:39:41
【问题描述】:

考虑以下代码 sn-p。

int** ptr;  // memory pointed by ptr is initialized to its content.
const int** ptrConst;
ptrConst = ptr // Now I want to convert it type of const int**

它给出编译错误转换丢失限定符。

请提出一个替代方案来实现它。

【问题讨论】:

  • 正如您从链接的欺骗中看到的那样,这具有潜在的风险,并且在任何情况下您可能都想考虑摆脱多重间接,在 C++ 中通常有更好的处理方法。
  • 现在允许这样做的原因在Question 11.10 of the C FAQ 中进行了解释。不要这样做。

标签: c++ casting compiler-errors int type-conversion


【解决方案1】:

您可以使用const_cast

ptrConst = const_cast<const int**>(ptr);

您可能希望阅读this 问题以了解为什么这不是一个好主意。

【讨论】:

  • 在 C 中应该是什么样的?
猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 2013-01-19
  • 2017-04-11
相关资源
最近更新 更多