BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #42772同步于 2010/8/27
CPP机器人发帖

[合集] [求助]类的静态成员数组如何初始化

shenlei
2010/8/27镜像同步0 回复
☆─────────────────────────────────────☆ poppick (eeee) 于 (Thu Apr 3 11:36:50 2008) 提到: 比如 class a { private : static int x[900] ; } ; 初始化为1 menset 提示是私有成员 一个一个初始化又太累 for循环初始在函数外不让用??(是这样吧?) ☆─────────────────────────────────────☆ Xer (SL小分队|小x|benogy) 于 (Thu Apr 3 11:43:24 2008) 提到: 设计一个成员函数,在成员函数里调用memset 【 在 poppick (eeee) 的大作中提到: 】 : 比如 : class a : { : ................... ☆─────────────────────────────────────☆ poppick (eeee) 于 (Thu Apr 3 12:06:16 2008) 提到: 可是这样的话就是说 这个静态成员的初始化实际要先构件一个对象了 可是正常应该是不需要构建对象的吧 而且menset只能把所有的出事话成相同的值 如果是初始成1,2,3...呢 而且这样给人一种怪怪的感觉 读代码的人可能会不明白吧 ☆─────────────────────────────────────☆ salaha (whitebear) 于 (Thu Apr 3 12:32:05 2008) 提到: 一个一个初始化 ☆─────────────────────────────────────☆ guying (宙斯) 于 (Thu Apr 3 14:02:48 2008) 提到: 这样不可以吗? for (int i=0;i<200;i++) a::x=value; ☆─────────────────────────────────────☆ poppick (eeee) 于 (Thu Apr 3 15:04:19 2008) 提到: 【 在 guying 的大作中提到: 】 : 这样不可以吗? : for (int i=0;i<200;i++) : a::x=value; 不可以啊 因为静态数组初始化是在函数外执行的 而函数外不能有独立的for语句的 ☆─────────────────────────────────────☆ windam (棒棒糖) 于 (Thu Apr 3 15:31:45 2008) 提到: 写个类的静态函数 ☆─────────────────────────────────────☆ poppick (eeee) 于 (Thu Apr 3 16:14:23 2008) 提到: 【 在 windam 的大作中提到: 】 : 写个类的静态函数 哦..正解啊 谢谢 原来以前一直把静态函数理解错了 ☆─────────────────────────────────────☆ kissmarsh () 于 (Thu Apr 3 18:59:03 2008) 提到: static array initialization Chapter 8 introduced the static const variable that allows you to define a constant value inside a class body. It’s also possible to create arrays of static objects, both const and non-const. The syntax is reasonably consistent: //: C10:StaticArray.cpp // Initializing static arrays in classes class Values { // static consts are initialized in-place: static const int scSize = 100; static const long scLong = 100; // Automatic counting works with static arrays. // Arrays, Non-integral and non-const statics // must be initialized externally: static const int scInts[]; static const long scLongs[]; static const float scTable[]; static const char scLetters[]; static int size; static const float scFloat; static float table[]; static char letters[]; }; int Values::size = 100; const float Values::scFloat = 1.1; const int Values::scInts[] = { 99, 47, 33, 11, 7 }; const long Values::scLongs[] = { 99, 47, 33, 11, 7 }; const float Values::scTable[] = { 1.1, 2.2, 3.3, 4.4 }; const char Values::scLetters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }; float Values::table[4] = { 1.1, 2.2, 3.3, 4.4 }; char Values::letters[10] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }; int main() { Values v; } ///:~ ☆─────────────────────────────────────☆ kissmarsh () 于 (Thu Apr 3 19:03:51 2008) 提到: 上面是 thinking in c++ 2nd ed vol one 第10章的一段 其实在static array member 的defination的时候(类内部的第一次出线数组名是声明,不是定义),就算不主动用={。。。}的语句赋值,数组也已经被初始化了。所以初始化不会发生在类的static member function里边。但是可以在里边进行赋值。
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。