首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

下面的例子const的作用是什么?该如何处理

2012-03-04 
下面的例子const的作用是什么?#include iostreamusingnamespacestdvoidmain(){constintSIZE5//const

下面的例子const的作用是什么?
#include <iostream>
using   namespace   std;
void   main(){
const   int   SIZE=5;       //const的作用是什么?如果没有const会有什么后果?
double   Array[SIZE];
cout < < "Enter " < <SIZE < < "numbers:\t ";
for(int   i=0;i <SIZE;i++;)
cin> > Array[i];
cout < < "In   reverse   order:;   "
for(i=SEZE-1;i> =0;i--)
cout < < " " < <Array[i]
}

[解决办法]
如果这样声明const int SIZE=5,C++编译器在编译在编译的时候给SIZE并不分配内存,直接用5换掉SIZE,即const int SIZE=5; double Array[SIZE]跟double Array[5]编译以后的效果一样,编译以后的代码SIZE将不存在。但在C中const int SIZE=5; double Array[SIZE]这样写是错误的。
[解决办法]
haihainie18() :“如果这样声明const int SIZE=5,C++编译器在编译在编译的时候给SIZE并不分配内存,直接用5换掉SIZE,即const int SIZE=5;”

不分配内存啊????
[解决办法]
c标准的const 和c++标准的const不一样

c++的编译器通常不为普通const常量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储与读内存的操作,使得它的效率也很高,同时,这也是它取代预定义语句的重要基础。

在c中的const修饰的变量还是变量

热点排行