How do you allocate a 2D array of pointers to objects?
Currently I have:
file.h
extern ClassName **c;
file.cpp
ClassName **c;
int main(){
// some stuff
c = new ClassName*[sizex];
for(int i = 0; i < sizex; ++i){
c[i] = new ClassName[sizey];
}
for(int i = 0; i < sizex; ++i){
for(int j = 0; j < sizey; ++j){
c[i][j] = new ClassName();
}
}
Which fails to compile with error's stating that there is no match for operator= using ClassName and ClassName*, which looking at the error makes sense. But if I were to change the assignment of c[i][j] to
ClassName cn();
c[i][j] = cn;
It gives a plethora of other errors. The array's size cannot be known until runtime (read from stdin) and it must also be extern. What is the proper way to declare an array in this case?
std::vector<ClassName*>as the double-pointer is only necessary in a C-style array of arrays.