array

hi pros,
i have a coding:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class a{
private:
 int x;
 int y;
public:
 a(int x1, int y1):x(x1),y(y1){}
};

class b{
private:
 a aa[10];
public:
 b(a z[]):aa(z){}
};


i get error from my "b" constructor, can tell me how am i going to assign aa[10] in class b using the constructor?
you can't initialize the variable aa in the constructor like that. think about it, there's nothing to enforce z to be an array of size 10. you can either memcpy the from z to aa or use a for loop to assign each element in z to the right index in aa.
Last edited on
1
2
3
4
for(int i =0; i < 10; i++)
{
  aa[i] = z[i];
}

or memcpy(aa,z,sizeof(aa));.

I would prefer the loop in this situation
Prefer std::copy, safer
std::copy(z, z+10, aa);
Oh, and try not to use arrays, use std::vector or boost::array instead
http://cplusplus.com/reference/stl/vector/
http://www.boost.org/doc/libs/1_43_0/doc/html/array.html
oh..thx alot for the answers
problem solved!~
Topic archived. No new replies allowed.