需求:
int c = 0;
// the value of c is initialized here
switch(someVar) {
case foo: c = 3; break;
case bar: c = 4; break;
default : c = 42; // what else?
}
// now c is constant
ASSUME_CONST_FROM_NOW(c) // 中途产生const变量
方法1:
c++0x
int const c = []() -> int {
int r;
switch(42) {
case 3:
r = 1; break;
case 4:
r = 2; break;
default:
r = 23;
};
return r;
}();
方法2:
int getInitCValue(int const& someVar)
{
// the value of c is initialized here
switch(someVar)
{
case foo: return 3;
case bar: return 4;
default : return 42; // what else?
}
}
int const c = getInitCValue(someVar);
方法3:
struct ConstValues
{
ConstValues()
{
switch(....)
// Initialize C/D
}
int const& getC() const {return c;}
int const& getD() const {return d;}
private:
int c;
int d;
};
分享到:
相关推荐
Const,Const函数,Const变量,函数后面的Const Const,Const函数,Const变量,函数后面的Const
在头文件的类的定义中定义了一个const成员变量c++ 规则: 1、类定义中不能进行初始化,因为头文件中类的定义只是一个声明,并没有分配真正空间,因此变量是不存在的,因此是不能赋值的。 2、const 定义的变量是不能...
C语言在头文件中定义const变量详解 在头文件中定义const不会有多变量的警告或错误,如果该头文件被大量包含会造成rom空间的浪费。 通过查看*.i文件的展开呢,可以发现每个.i文件都会有相应的变量展开。 查看*.map...
Const,Const函数,Const变量,函数后面的Const[参考].pdf
2、C++中const变量.docx
详细说明了全局变量,局部变量之前用static,extern,const修饰后的不同用法
Const指针变量只保护其Value值不被改变,并不能保护指针所指向的地址中的值不被改变,下面一起来学习一下
为了程序的健壮型,减少程序出错,这里详细介绍了const的用法。
c/c++语言中const的用法,const 定义变量,指针的用法和不同
const的一些操作
逆向dll中的函数及通过函数api钩子获取dll中的全局或是const变量值
详细介绍了define与const定义变量的用法,讲出了两者的区别。
static,const,static const,const static是c++中几个常见的很容易弄混淆的几个概念,在这里总结了一下,与大家分享!
const和let一样,也是ES6版本中引入的新关键字,下面我们就通过例子来简单掌握JavaScript中const关键词声明常量与变量的用法
C#变量和const常量的使用
extern、static修饰变量的使用及define和const区别可结合博客http://blog.csdn.net/error/404.html?from=http%3a%2f%2fblog.csdn.net%2faiyang10%2farticle%2fdetails%2f49448591理解
主要介绍了ES6 let和const定义变量与常量的应用,结合实例形式分析了ES6使用let定义变量以及使用const定义常量的相关操作技巧,需要的朋友可以参考下