中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
【原创】(学习笔记)静态常量成员变量的初始化
作者:未知 时间:2005-07-27 23:31 出处:CSDN 责编:chinaitpower
              摘要:【原创】(学习笔记)静态常量成员变量的初始化

/*
主题:静态常量成员变量的初始化:
书名:<<The C++ Standard Library -A Tutorial and Reference>>

P25 (2.2.8 Initialization of Contants Static Members)
*******************************************************************
It is now possible to initialize integral constant static members inside the class structure.
This is useful when the constants is used in the class structure after the initialization.
(对于当这个常量会在初始化后要使用的情况,这样做会比较有效)

For example ===>
    class MyClass{
        static const int NUM = 100;
        int elements[NUM];
        ...
    };

Note that you still have to to define space for a constant static member that is initialized within a class definition:
  const int MyClass::NUM;         //no initialization here
*/

#pragma warning(disable:4530)
#include <iostream>

using namespace std;

class A
{
public:
A(){
  for(int i=0; i<SIZE; i++)
   num[i] = i;
}
void print(){
  for(int i=0; i<SIZE; i++)
   cout<<num[i]<<" "<<endl;
}
private:
static const SIZE = 10;
//注意:由于SIZE作为一个const常量,所以必须在首次定义的时候就给它赋值。
int num[SIZE];
};

const int A::SIZE;
//注意:由于SIZE作为一个类的静态成员,应该在类体外部定义(以取得和全局变量类似的效用)
int main()
{
A a;
a.print();
return 0;
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有