Entering array-index in class at runtime: possible?

I have a class: indiv
The "workhorse" part of the data is a 2-dimensional array: int chromosome[19][2]
To make the code more flexible, I would like to be able to make the first index changeable at runtime, along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

class indiv {
int n;
int chromosome[n][2];
public:
indiv(n){};
// other methods
};

int main() {
int chr;
cout<<"How many pairs of chromosomes does this species have?\n";
cin>>chr;
indiv PM(chr);
// rest of program
return 0;
}


This code produces a whole catalogue of errors [I'm not asking it to be debugged, just including it to give an idea of what I am trying to achieve] and it could be that what i am trying to do is impossible.I have not found any reference in tutorials of how to do it, but also no statement therein telling me I can't. I could do it if the array were in main rather than in class, so I am hopeful.
So,
Q1: is this possible?
Q2: if so, can anyone point me to a reference for the correct syntax for doing it?

Thanks.
Last edited on
Q1: is this possible?
No. An array needs to be dynamically allocated when it is changed at runtime. For dynamic arrays you may use vector:

http://www.cplusplus.com/reference/vector/vector/?kw=vector


If you want different array sizes at compile time it is actually possible using template.
Thanks. I'll get reading :)
Topic archived. No new replies allowed.