What does compile time 'const' mean?

后端 未结 6 1449
执笔经年
执笔经年 2020-12-10 14:49

They say the difference between readonly and const is that const is compile-time (while readonly is run time). But what exactly does that mean, The fact that it\'s compile t

6条回答
  •  余生分开走
    2020-12-10 15:02

    Although the answer that Julio provided is valid from the effects of setting a variable as a constant or a read only, there are a great deal of difference between the two declarations.

    While many people simply state the obvious that the value of a constant is resolved at the compilations, while a read only value will be resolved only during the run time, the main point resides where the reference is quept.

    Depending on the data type of the constant, it will be either replaced at the command evocation, or stored in the HEAP and referenced by a pointer.

    For instance, the code:

    const int x = 3;
    int y = 3 * x;
    

    may be resolved at the compilation time as simply:

    int y = 3 * 3;
    

    On the other hand a read only field is always stored on the STACK and referenced by a pointer.

提交回复
热议问题