【问题标题】:Returning a const double pointer to const data [duplicate]返回一个指向常量数据的常量双指针[重复]
【发布时间】:2016-01-16 05:58:57
【问题描述】:

我有一个int** matrix,我想以只读形式返回它。

所以,我会这样做:

int const ** const get_matrix() {
  return (int const ** const)matrix;
}

但现在我尝试这样做:

int const ** const get_matrix() {
    return static_cast<int const ** const>(matrix);
}

得到:

从“int**”类型到“const int** const”类型的无效静态转换

为什么?

【问题讨论】:

  • Const_cast 不起作用?我会先试试
  • 是的@rholmes,我不知道!我把它标记为骗子。 :)

标签: c++ pointers c++11 casting constants


【解决方案1】:

试试这个:

int const ** const get_matrix() {
    return const_cast<int const ** const>(matrix);
}

它正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 2011-03-27
    相关资源
    最近更新 更多