How to return const Float** from a C++ function
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;
}
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**.
Why? How can I avoid table to be modified out side of the class?
Declare your method like this:
float const* const* getTable() const {return table;}
or
const float* const* getTable() const {return table;}
if you prefer.
You can't assign a float**
to a float const**
because it would allows to modify a const object:
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++ rule is that when you add a const before a star, you have to add a const before each following one.
C rule is that you can only add a const before the last star.
In both languages, you can remove a const only before the last star.
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
链接地址: http://www.djcxy.com/p/21244.html上一篇: 基类和派生类构造函数的内存分配