C + + class array Batch assignment value

Source: Internet
Author: User

Classes and structs are different, structs can be initialized with {...} method is all assigned, but what about the struct? One is to write an array of data into the same structure, and then the For loop uses a non-constructor to write to the class array. Another method is to write directly to the corresponding array, for example, with one of the following classes:

Class a{

char* pname;

int property[5];

Public

void IPT (char* t,int pt[]) {pname=t;

for (int i=0;i<5;i++)

Property[i]=pt[i];

}

}

Use the first method as follows:

struct ac{

char* pname;

int property[5];

} inita[2]={{"A", 1,2,3,4,5},{"B", 2,3,4,5,6}};

This allows the IPT to be called:

A;

for (int i =0;i< 2;i++)

A.ipt (inita[i].pname,inita[i].property[]);

If you use the second method, you do not have to define the structure, as follows:

int pr[2][5]={{1,2,3,4,5},{2,3,4,5,6}};

char* pm[2]={"A", "B"};

Then use the For loop directly:

for (int i=0;i<2;i++)

A.ipt (Pm[i],pr[i]);

Which one do you think is simpler?

C + + class array Batch assignment value

Related Article

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.