如何" const的"在C和C ++有什么不同? [英] How does "const" differ in C and C++?

查看:145
本文介绍了如何" const的"在C和C ++有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在变数const的资格,C和C ++有什么不同?

从:<一href=\"http://stackoverflow.com/questions/4486326/does-const-just-mean-read-only-or-something-more-in-c-c\">Does &QUOT; const的&QUOT;仅仅意味着只读或更多的东西? (在C / C ++)

是什么促使这个问题是这样的回答:<一href=\"http://stackoverflow.com/questions/4024318#4024417\">http://stackoverflow.com/questions/4024318#4024417他指出常量只是手段只读C.我以为这就是全部常量的意思,不管它是否是C或C ++。他是什么意思?


解决方案

用C常量不能用来建恒恩pressions

例如:

 的#include&LT;&stdio.h中GT;
诠释的main()
{
   INT I = 2;
   const int的C = 2;
   开关(ⅰ)
   {
      案例C:的printf(你好);
      打破;      默认:输出(世界);
   }
}

在C不工作,因为case标签不会减少整型常量。

How does the const qualification on variables differ in C and C++?

from: Does "const" just mean read-only or something more? (in C/C++)

"What prompted this question was this answer: http://stackoverflow.com/questions/4024318#4024417 where he states const "just" means read-only in C. I thought that's all const meant, regardless of whether it was C or C++. What does he mean?"

解决方案

const in C cannot be used to build constant expressions.

For example :

#include <stdio.h>
int main()
{
   int i = 2;
   const int C = 2;
   switch(i)
   {
      case C  : printf("Hello") ;
      break;

      default : printf("World");
   }
}

doesn't work in C because case label does not reduce to an integer constant.

这篇关于如何&QUOT; const的&QUOT;在C和C ++有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆