类里面的const:编译期间的变量和常量
2008-12-12 10:28
267 查看
//类里面的const:编译期间的变量和常量。
#include<iostream>
#include<string>
using namespace std;
//var in compile-time:
class Fred {
const int size;
public:
Fred(int sz);
void print();
};
//对size的初始化在参数列表和函数体之间,保证在使用它之前为常量。
Fred::Fred(int sz):size(sz){}
void Fred::print(){ cout << size << endl; }
//constant in comiple-time:
//一个存放字符串指针的栈StringStack:
class StringStack{
//keyword:static
static const int size = 100;//enum {size = 100};亦可,“匿名枚举”
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};
StringStack::StringStack():index(0){
memset(stack, 0 ,size*sizeof(string*));
}
void StringStack::push(const string* s){
if(index < size)
stack[index++] = s;
}
const string* StringStack::pop(){
if(index > 0){
const string* t = stack[--index];
stack[index] = 0;
return t;
}
return 0;
}
//StringStack测试用例:
string iceCream[] = {
"pralines & cream",
"fudge ripple",
"wild mountain",
"raspberry sorbet",
"rocky road"
};
//calculat the size of the case:
const int iCsz =
sizeof iceCream / sizeof *iceCream;
//Test in main():
int main(){
//class Fred:
Fred a(1),b(2),c(3);
a.print(),b.print(),c.print();
//class StringStack:
StringStack ss;
for(int i = 0; i < iCsz; ++i)
ss.push(&iceCream[i]);
const string* p;
while((p = ss.pop()) != 0)
cout << *p << endl;
//display the result
system("pause");
}
/** 对对象的数组初始化的三种方法
@for循环
@构造函数
@<string.h>的memset(,,)【该函数以字节为操作单位】
*/
#include<iostream>
#include<string>
using namespace std;
//var in compile-time:
class Fred {
const int size;
public:
Fred(int sz);
void print();
};
//对size的初始化在参数列表和函数体之间,保证在使用它之前为常量。
Fred::Fred(int sz):size(sz){}
void Fred::print(){ cout << size << endl; }
//constant in comiple-time:
//一个存放字符串指针的栈StringStack:
class StringStack{
//keyword:static
static const int size = 100;//enum {size = 100};亦可,“匿名枚举”
const string* stack[size];
int index;
public:
StringStack();
void push(const string* s);
const string* pop();
};
StringStack::StringStack():index(0){
memset(stack, 0 ,size*sizeof(string*));
}
void StringStack::push(const string* s){
if(index < size)
stack[index++] = s;
}
const string* StringStack::pop(){
if(index > 0){
const string* t = stack[--index];
stack[index] = 0;
return t;
}
return 0;
}
//StringStack测试用例:
string iceCream[] = {
"pralines & cream",
"fudge ripple",
"wild mountain",
"raspberry sorbet",
"rocky road"
};
//calculat the size of the case:
const int iCsz =
sizeof iceCream / sizeof *iceCream;
//Test in main():
int main(){
//class Fred:
Fred a(1),b(2),c(3);
a.print(),b.print(),c.print();
//class StringStack:
StringStack ss;
for(int i = 0; i < iCsz; ++i)
ss.push(&iceCream[i]);
const string* p;
while((p = ss.pop()) != 0)
cout << *p << endl;
//display the result
system("pause");
}
/** 对对象的数组初始化的三种方法
@for循环
@构造函数
@<string.h>的memset(,,)【该函数以字节为操作单位】
*/
相关文章推荐
- 类里面的const:编译期间的变量和常量
- 常量和变量在编译期间的行为
- 参数中有const时进行数据类型的转换问题-固定变量转常量
- 条款2:运行时常量(readonly)优于编译时常量(const)
- 常量与只读变量的区别(const)
- 《Effective C#》条款2:运行时常量(readonly)优于编译时常量(const)
- C语言返璞归真之修改const常量和使用变量定义数组长度
- 单引号里面的变量或常量不会再被解析
- 正确使用iOS常量(const)、enum以及宏(#define)在iOS开发中请尽量多使用const、enum来代替宏定义(#define);随着项目工程的逐渐增大,过多的宏定义还可能影响项目的编译
- Effective C# Item2:运行时常量(readonly)优于编译时常量(const)
- 运行时常量(readonly)优于编译时常量(const)
- 改善C#程序的50种方法 条款2:运行时常量(readonly)优于编译时常量(const)
- 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
- 条款2:运行时常量(readonly)优于编译时常量(const)
- Const 编译时常量
- const(每个对象中的常量), static const(类的编译时常量)
- Effective C# Item2:运行时常量(readonly)优于编译时常量(const)
- [Wondgirl]ECMAScript6(ES6)(二)变量let、var和常量const的定义和区别
- IOS 宏(define),变量 ,常量(const)那些事儿
- C语言中const只读变量及常量的注意事项