免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10126 | 回复: 7
打印 上一主题 下一主题

[C++] 问:C++能不能在类中定义一个私有的静态常量数组? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-25 11:13 |只看该作者 |倒序浏览
如题,有点晦涩了,举个例子,想定义一个"学生"类如下:
//file ClassMate.h
string District[10]={"朝阳","海淀","宣武","东城","怀柔","密云"};//实际程序用拼音
class classmate{
private:
        string mName;
        int mHomeDistrictIndex;
public:
        classmate(){};
        .......;
};

我想,这里的string District[10]只供ClassMate类使用,不想让程序别的类也能看见并访问它,不知道该怎么解决?

大家别说我偏激啊,哈哈,我只是想,C++的一个思想是数据隔离,能隐藏的就隐藏.不知道这个该怎么隐藏呢?

我搞成了这个样子,mingw下报错:
class classmate{
private:
        static string District[10]={"朝阳","海淀","宣武","东城","怀柔","密云"};
        string mName;
        int mHomeDistrictIndex;
public:
        classmate(){};
        .......;
}

请大家指教!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-06-25 11:28 |只看该作者
static const string District[10]
这样试试。

论坛徽章:
0
3 [报告]
发表于 2007-06-25 11:38 |只看该作者
class Operator:public BaseStructure{
private:
        OperatorType mType;
        static int ar[6];
        static const string OperatorSymbol[7]={"+","-","*","/","^","??"};
public:
..........
}
结果提示错误:
Operator.hxx:13: error: invalid in-class initialization of static data member
of non-integral type `const std::string[7]'

论坛徽章:
0
4 [报告]
发表于 2007-06-25 12:52 |只看该作者
any idea?

论坛徽章:
0
5 [报告]
发表于 2007-06-25 12:53 |只看该作者
在类外初始化
class Operator:public BaseStructure{
private:
        OperatorType mType;
        static int ar[6];
        static const string OperatorSymbol[7];
public:

}

const string Operator:OperatorSymbol[]={"+","-","*","/","^","??"};

论坛徽章:
0
6 [报告]
发表于 2007-06-25 13:00 |只看该作者
静态数据在c++中只有 static const int t = 0;
可以初始化。
其他都要在类的外面。

论坛徽章:
0
7 [报告]
发表于 2007-06-25 13:23 |只看该作者
very thanks!

论坛徽章:
0
8 [报告]
发表于 2007-06-25 15:27 |只看该作者
原帖由 fiowrf 于 2007-6-25 13:23 发表
very thanks!


应该用many thanks才对 ^_^
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP