class question

closed account (STR9GNh0)
i am doing class.

i need to create a dynamic array in stack on classes


abc x[100]; <- is there a way to change 100 to a variable?


my error is: ISO C++ forbids variable-size array 'x'
Last edited on
A few ways. The simplest is to use a vector instead of an array:

1
2
3
int size = 100;

std::vector<abc> x(size);


Another way is to dynamically allocate, but then you have to remember to delete it:

1
2
3
4
5
6
abc* x = new abc[size];

// ...

// be sure to do this when you're done, or you will leak memory:
delete[] x;
closed account (STR9GNh0)
thanks
Topic archived. No new replies allowed.