// Struct mystruct {int x, y, z ;}; mystruct table [] ={{, 3 },{, 6 },... // etc };// |
Answer: This question depends entirely on how you think about C ++. One of the advantages of C ++ is that it forces you to do the right thing. For example, C ++ does not like to call constructors when creating objects. This is only the reason why you Cannot initialize class objects with raw data, whether it is partial arrays or other data. The purpose of the constructor is to ensure that every object is correctly initialized, whether fromProgramStack, available memory heap allocation space, or as a static array element. Let the original data bypass the constructor to be taboo. This also prevents you from using initial data to create static object arrays-you must call the constructor! |
// 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 (, 3), cfooble (, 6 ), cfooble (0), // can use any constructor !}; // |
/// staticclassarray-describes how to initialize a static C array on a C ++ object // the compilation method is as follows: /// Cl fooble. CPP // # include
//// // a typical class -- has three data members... // class cfooble {protected: int x, y, z; public: // two constructors... cfooble (int I) {x = y = z = I;} cfooble (INT XX, int YY, int zz): X (XX), y (yy ), Z (zz) {}// an output function void print () {printf ("cfooble % P: (% d, % d, % d)/n ", this, x, y, z);} // check whether this function is empty... int isempty () {return x = 0 & Y = 0 & Z = 0 ;}}; # ifdef never // The following Code cannot be run.-initialization of C ++ class objects cannot be performed "Bluntly! Cfooble table [] = {1, 2, 3}, {4, 5, 6}, {0, 0}; # endif // how to initialize an array of classes: cfooble table [] = {cfooble (, 3), cfooble (, 6), cfooble (0), // You can even use different constructors !}; Void main () {for (cfooble * Pc = table ;! PC-> isempty (); PC ++) {PC-> Print () ;}///
|