Dynamic array initialization

Jan 14, 2012 at 11:43am
Hello everyone,

I am working on A Star algorithm and I have run into problem I am not sure how to realize. I am loading map from file of chars, where '*' represents walkable node and ' ' represents non-walkable. I load the map into 2dim char array, dynamically allocated. The loading and parsing works just fine, but I need to return my own object - Map, which is build of 2dim array of Nodes (my own object).

Since I need to dynamically create the 2dim array of Nodes, it would be awesome to intialize them already too, since I know all the information for initialization, but I don't know how. I need to pass them x, y and walkable flag - is this possible during creating the array, or do I really have to create the array first and then loop through it and make some method "setNode(...)" to initialize the nodes again?

Hope that makes any sense.

Thanks in advance.
Jan 14, 2012 at 10:35pm
A small piece of code would be good. But I am following you I think you will have to init each one of the elements of the array as you talked about.
Jan 15, 2012 at 4:07am
If your Nodes are structs then you will need a loop to initialize values; if they're class objects then your default constructor should explicitly set default values, and you should consider an explicit constructor taking (int,int,bool) arguments or whatever you need.
Jan 15, 2012 at 4:36am
Use a container.
By instance
1
2
3
4
std::vector< foo > v;
v.reserve(size);
for(size_t K=0; K<size; ++K)
  v.push_back( foo(args) );
Topic archived. No new replies allowed.