Why am I getting an error converting a ‘float**’ to ‘const float**’?

前端 未结 4 2088
旧巷少年郎
旧巷少年郎 2020-11-29 07:26

I have a function that receives float** as an argument, and I tried to change it to take const float**.

The compiler (g++) did

4条回答
  •  北海茫月
    2020-11-29 08:07

    If you converted the parameter to const float** you could then store a const float* at the memory location where the parameter points to. But the calling function thinks that this memory location is supposed to contain a non-const float* and might later try to change this pointed-to float.

    Therefore you cannot cast a float** to a const float**, it would allow you to store pointers to constants in locations where pointers to mutable values are expected.

    For more details see the C++ FAQ Lite.

提交回复
热议问题