类里面的const:编译期间的变量和常量
2013-10-18 14:52
176 查看
//类里面的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(,,)【该函数以字节为操作单位】
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/SearchLife/archive/2008/12/12/3502441.aspx
#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(,,)【该函数以字节为操作单位】
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/SearchLife/archive/2008/12/12/3502441.aspx
相关文章推荐
- 类里面的const:编译期间的变量和常量
- 常量和变量在编译期间的行为
- 条款2:运行时常量(readonly)优于编译时常量(const)
- [Wondgirl]ECMAScript6(ES6)(二)变量let、var和常量const的定义和区别
- 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
- const定义的变量,可以作为数组[ ]里面的值吗?
- C++编译期间类中的常量
- Const变量和enum变量gcc编译生成目标文件分析
- const 引出的对常量,变量,只读变量的讨论
- C#中的readonly(运行时常量)与const(编译时常量)的区别
- C/C++语法知识精华整理(1)-左值与右值、变量与常量、 define与const、Printf特点、补码与阶码、位运算等
- 常量和常变量(const)
- iOS中宏(define)/常量(const)/变量的理解
- 通过常量表达式初始化的const变量定义在头文件中
- 常量与只读变量的区别(const)
- C语言中const只读变量及常量的注意事项
- const常量用extern声明定义的问题(extern变量不能在使用类里初始化)
- c/c++中extern、static、const的用法及全局变量/常量
- 编译时常量和编译时变量
- 《Effective C#》条款2:运行时常量(readonly)优于编译时常量(const)