Why do I get an error about the initializer not being a constant?

后端 未结 6 1834
失恋的感觉
失恋的感觉 2021-01-17 17:49

I am using the following code.

const int X_ORIGIN = 1233086;             
const int Y_ORIGIN = -4728071;              
const int Z_ORIGIN = 4085704;
const in         


        
6条回答
  •  长发绾君心
    2021-01-17 18:15

    Often people are mislead by the naming of the keyword const, implying something of a constant value that can't be changed. In C at least, it means readonly. const qualified objects at file scope are not having the proper constness to serve as array initializers.

    As an example for non-constant constness, it is perfectly ok to declare

     const volatile unsigned int milliseconds_since_boot;
    

    being a value that gets updated from outside the compiler's control (think HW register) and that you are not allowed to assign to, i.e. it is readonly.

提交回复
热议问题