Initialize Class Object

Source: Internet
Author: User
In C ++, how does one initialize table data in the class? In C, I can initialize the structure array as follows:
// Struct mystruct {int x, y, z ;}; mystruct table [] ={{, 3 },{, 6 },... // etc };//
However, if mystruct is a C ++ class instead of a structure, I will make an error during compilation. I think this is a deficiency of C ++.
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 !}; //
The followingCodeIs a complete example that can be compiled.
 /// 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 () ;}/// 
     
in C ++, you can use any constructor to initialize array elements, in addition, C ++ even uses the default constructor to initialize additional elements instead of external initialization routines. For me, this is an improvement, not a deficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.