const变量的使用方法。。

xiaoxiao2021-02-28  82

 

一句话:

       只有const的定义前面才可以有extern;别的则可有可无。

 

两种想法:

 

1.如果想在头文件中定义const:

         const  int a=10;

2.如果想在cpp文件中进行定义:

        前面使用extern 将其扩大为全局变量。

      头文件中写:

        extern const int a;

     在cpp文件中进行定义:

        extern const int a=10;

 

这样的话,const变量的使用就和普通变量一样了,

 

3.只有const有这个“文件作用域”,实际上是基于分段编译的后果。

 

 

 

转载请注明原文地址: https://www.6miu.com/read-73012.html

最新回复(0)