我有一个包含数组" float ** table"的类.现在我希望有成员函数来返回它,但不希望它在类之外被修改.所以我这样做了:
class sometable
{
public:
...
void updateTable(......);
float **getTable() const {return table;}
private:
...
float **table;
}
Run Code Online (Sandbox Code Playgroud)
当我使用常量对象调用getTable时,这会编译好.现在我试图通过将getTable声明为" const float **getTable()" 来使其更安全.我收到以下编译错误:
Error:
Cannot return float**const from a function that should return const float**.
Run Code Online (Sandbox Code Playgroud)
为什么?如何避免将表修改为类的一部分?
像这样声明你的方法:
float const* const* getTable() const {return table;}
Run Code Online (Sandbox Code Playgroud)
要么
const float* const* getTable() const {return table;}
Run Code Online (Sandbox Code Playgroud)
如果你更喜欢.