简体   繁体   中英

Is this an array initialization?

int ia[] = {0,1,2,3,4,5,6,7,8,9}; // ia is an array of ten ints
auto ia2(ia); // ia2 is an int* that points to the first element in ia
ia2 = 42;     // error: ia2 is a pointer, and we can't assign an int to a pointer

This is a piece of code on C++ Primer. Could someone explain to me what does the second line mean. Is it a way of initialization? Where could I find this kind of initialization? I searched a lot but still couldn't get the relevant information. A link is also welcome. Many thanks!

This is copy initialization. For the base types, it is the same as using = :

int k = 42; 
//is the same as : 
int k(42);

This means that line #2 could be rewritten to the following and still have the same meaning :

auto ia2 = ia;

auto will be deducted to an int* here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM