1
votes

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?

4
Pointer to pointer to pointer to..... Looks like a bad design. This is C++ - use std::vector and return references.Kirill V. Lyadvinsky

4 Answers

6
votes

Declare your method like this:

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

or

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

if you prefer.

4
votes

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.

2
votes

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

1
votes

Though you can clearly type the syntax just like that, I find it much more readable to define some typedefs for multiple-dimension arrays.

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_; }
};