//struct MYSTRUCT { int x,y,z;};MYSTRUCT table[] = { { 1,2,3 }, { 4,5,6 }, ... // etc};// |
//class CFooble { int x,y,z;public: CFooble(int xx, int yy, int zz) : x(xx),y(yy),z(zz) { ... } CFooble(int i) { x=y=z=i; }};CFooble table[] = { CFooble(1,2,3), CFooble(4,5,6), CFooble(0), // can use any constructor!};// |
//// StaticClassArray — 說明如何初始化在C++對象的靜態C數組 // 編譯方法如下://// cl fooble.cpp//#include <stdio.h>//////////////////// 一個典型的類——有三個資料成員...//class CFooble {protected: int x,y,z;public: // 兩個建構函式... CFooble(int i) { x=y=z=i; } CFooble(int xx, int yy, int zz) : x(xx),y(yy),z(zz) { } // 一個輸出函數 void print() { printf("CFooble at %p: (%d,%d,%d)/n", this, x, y, z); } // 這個函數檢查是否為空白... int IsEmpty() { return x==0 && y==0 && z==0; }};#ifdef NEVER// 如下這樣將不能運行—不能“生硬”地進行C++類對象的初始化!CFooble table[] = { { 1,2,3 }, { 4,5,6 }, { 0,0,0 }};#endif// 以下是如何初始化一個類數組:CFooble table[] = { CFooble(1,2,3), CFooble(4,5,6), CFooble(0), // 甚至可以是用不同的構造器!};void main(){ for (CFooble* pc=table; !pc->IsEmpty(); pc++) { pc->print(); }}// |