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:
我有一个类,其中包含一个数组“float ** table”。现在我希望有成员函数来返回它,但不希望它在类之外被修改。所以我这样做了:
class sometable
{
public:
...
void updateTable(......);
float **getTable() const {return table;}
private:
...
float **table;
}
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:
当我使用常量对象调用getTable时,这会编译好。现在我试图通过将getTable声明为“const float ** getTable()”来使其更安全。我收到以下编译错误:
Error:
Cannot return float**const from a function that should return const float**.
Why? How can I avoid table to be modified out side of the class?
为什么?如何避免将表修改为类的一部分?
4 个解决方案
#1
Declare your method like this:
像这样声明你的方法:
float const* const* getTable() const {return table;}
or
const float* const* getTable() const {return table;}
if you prefer.
如果你更喜欢。
#2
You can't assign a float**
to a float const**
because it would allows to modify a const object:
你不能将float **赋给float const **,因为它允许修改const对象:
float const pi = 3.141592693;
float* ptr;
float const** p = &ptr; // example of assigning a float** to a float const**, you can't do that
*p = π // in fact assigning &pi to ptr
*ptr = 3; // PI Indiana Bill?
C and C++ rules differ about what is allowed.
C和C ++规则在允许的内容方面有所不同。
-
C++ rule is that when you add a const before a star, you have to add a const before each following one.
C ++规则是当你在星形之前添加一个const时,你必须在每个后面添加一个const。
-
C rule is that you can only add a const before the last star.
C规则是你只能在最后一个星之前添加一个const。
In both languages, you can remove a const only before the last star.
在这两种语言中,您只能在最后一颗星之前删除一个const。
#3
You could declare your method as
您可以将方法声明为
const float * const * const getTable() const {return table;}
but even this (the outermost const - next to the function name) would not prevent the client to try to delete it. You could return reference instead, but the best would be to use an std::vector for table and return const ref to it - unless using a C style array is a must
但即便如此(最外面的const - 函数名旁边)也不会阻止客户端尝试删除它。您可以返回引用,但最好的方法是使用std :: vector作为表并将const ref返回给它 - 除非使用C样式数组是必须的
#4
Though you can clearly type the syntax just like that, I find it much more readable to define some typedefs for multiple-dimension arrays.
虽然您可以清楚地键入语法,但我发现为多维数组定义一些typedef更具可读性。
struct M {
typedef double* t_array;
typedef const double t_carray;
typedef t_array* t_matrix;
typedef const t_carray* t_cmatrix;
t_matrix values_;
t_cmatrix values() const { return values_; }
t_matrix values() { return values_; }
};
#1
Declare your method like this:
像这样声明你的方法:
float const* const* getTable() const {return table;}
or
const float* const* getTable() const {return table;}
if you prefer.
如果你更喜欢。
#2
You can't assign a float**
to a float const**
because it would allows to modify a const object:
你不能将float **赋给float const **,因为它允许修改const对象:
float const pi = 3.141592693;
float* ptr;
float const** p = &ptr; // example of assigning a float** to a float const**, you can't do that
*p = π // in fact assigning &pi to ptr
*ptr = 3; // PI Indiana Bill?
C and C++ rules differ about what is allowed.
C和C ++规则在允许的内容方面有所不同。
-
C++ rule is that when you add a const before a star, you have to add a const before each following one.
C ++规则是当你在星形之前添加一个const时,你必须在每个后面添加一个const。
-
C rule is that you can only add a const before the last star.
C规则是你只能在最后一个星之前添加一个const。
In both languages, you can remove a const only before the last star.
在这两种语言中,您只能在最后一颗星之前删除一个const。
#3
You could declare your method as
您可以将方法声明为
const float * const * const getTable() const {return table;}
but even this (the outermost const - next to the function name) would not prevent the client to try to delete it. You could return reference instead, but the best would be to use an std::vector for table and return const ref to it - unless using a C style array is a must
但即便如此(最外面的const - 函数名旁边)也不会阻止客户端尝试删除它。您可以返回引用,但最好的方法是使用std :: vector作为表并将const ref返回给它 - 除非使用C样式数组是必须的
#4
Though you can clearly type the syntax just like that, I find it much more readable to define some typedefs for multiple-dimension arrays.
虽然您可以清楚地键入语法,但我发现为多维数组定义一些typedef更具可读性。
struct M {
typedef double* t_array;
typedef const double t_carray;
typedef t_array* t_matrix;
typedef const t_carray* t_cmatrix;
t_matrix values_;
t_cmatrix values() const { return values_; }
t_matrix values() { return values_; }
};