Constructor array copy

In the below program, how can copy the array n[] into Array[]. The below is not working.. help please

#include <iostream>

using namespace std;

class arrayPlay
{
private:
int Array[];

public:
arrayPlay(int n[]);
};
arrayPlay::arrayPlay(int n[])
{
Array[] = n[];
int i;
for(i=0;i<3;i++)
{
cout<<Array[i];
}
}
int main()
{
int n[5] = {1,2,3};
arrayPlay *a = new arrayPlay(n);
return 0;
}
Whell... array with no size must be seen as "unwanted"... I think that the only reasonable array with no size defined can be only the second parameter of main

 
int main(int argc, char * argv[]) { ... }


In the other cases I would NEVER use unsized array becouse you can create issues out-of-your-control, and also, as you could see, it is very error-prone.

In order to meet your objective you can use two approaches.

The suggested approach is to use std::vector<int> instead of int[]. It is more efficient.
You can also use references to transmit a vector from main to class.

Another approach (not very suggested) is to use pointers instead of arrays. For example if you use int * n you could access to member 0 using n[0].
But be careful, becouse this approach is not so easy to mantain and, if used wrogly, can expose your program to errors hardly to find.
So, I repeat, it is better to use a std::vector<int>
Topic archived. No new replies allowed.