C ++ array member Initialization

Source: Internet
Author: User

In C ++, if an array is defined as a member of a class, initialization will occur.

 

As follows:

 

Class

{

Public:

A ();

~ A (){};

 

Int ABC [3];

}

 

The intuitive idea is:

 

A: ()

: ABC ({1, 2, 3 })

{

 

}

 

Sorry, you will see a syntax error during compilation.

 

Of course, the work und can be used here

 

A: ()

{

For (INT nloop = 1; nloop <= 3; nloop ++)

ABC [nloop] = nloop;

}

But what if we need to initialize an array of objects without the default structure? For example, Class B {public: B (int );~ B () {}; int m_nb;} B: B (int A): m_nb (){}

Class

{

Public:

A ();

~ A (){};

 

B ABC [3];

}

How should we initialize it? Here we can only use a work und.

Class

{

Public:

A ();

~ A (){};

 

B * ABC [3];

}

A: A () {ABC [0] = new B (1); ABC [1] = new B (2 ); ABC [2] = new B (3);} Of course, I may be too ignorant. If anyone knows the initialization syntax of the member array, please kindly advise.

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.