c++ - How do I pass a truly immutable 2D array to a function? -


i have function:

void foo(const double *const *matrix, unsigned num_rows, unsigned num_columns){     //matrix[0][0] = 5; // error: expression must modifiable lvalue     (unsigned = 0; < num_rows; i++) delete matrix[i];     delete matrix; } 

...wherein function cannot change values @ given index in matrix, yet still able delete matrix or of rows. there way pass in 2d matrix , guarantee not altered in way?

edit: doesn't there's way this, i'll switch using vectors.

no.

delete can called on pointer if it's passed const & or declared const because pointer not going changed.

there no way prevent doing

unsigned char *p = (unsigned char *)your_precious_data_pointer; *p = 42; 

get on it.

const correctness designed detect accidental mistakes, not preventing intentional actions programmers. it's not security.

it can debated if it's tool preventing accidental mistakes.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -