如何从C ++函数返回const Float ** [英] How to return const Float** from a C++ function

查看:165
本文介绍了如何从C ++函数返回const Float **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它持有一个数组 float ** table 。现在我想有成员函数返回它,但不希望它在类外部被修改。所以我这样做:

I have a class which hold an array "float ** table". Now I want to have member function to return it, but don't want it to be modified outside of the class. So I did this:

class sometable 
{
  public:
   ...
   void updateTable(......);
   float **getTable() const {return table;}
  private:
    ...
    float **table;
}



当使用常量对象调用getTable时,现在我尝试
通过将getTable声明为 const float ** getTable()使它更安全。我得到
以下编译错误:

This compiles OK when I call getTable with a constant object. Now I tried to make it safer by declaring getTable as "const float **getTable()". I got the following compilation error:

Error:
  Cannot return float**const from a function that should return const float**.

为什么?

推荐答案

如下声明你的方法:

float const* const* getTable() const {return table;}

const float* const* getTable() const {return table;}

这篇关于如何从C ++函数返回const Float **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆