How does one create and use a pointer to an array of an unknown number of structures inside a class?

Posted by user1658731 on Stack Overflow See other posts from Stack Overflow or by user1658731
Published on 2012-09-09T21:23:29Z Indexed on 2012/09/09 21:38 UTC
Read the original article Hit count: 254

Filed under:
|
|
|

Sorry for the confusing title...

I've been playing around with C++, working on a project to parse a game's (Kerbal Space Program) save file so I can modify it and eventually send it over a network. I'm stuck with storing an unknown number of vessels and crew members, so I need to have an array of unknown size.

Is this possible? I figured having a pointer to an array would be the way to go.

I have:

class SaveFileSystem 
{
    string version;
    string UT;
    int activeVessel;
    int numCrew;
    ??? Crews; // !!
    int numVessels;
    ??? Vessels; // !!
}

Where Crews and Vessels should be arrays of structures:

struct Crew 
{
    string name;
    //Other stuff
};

struct Vessel
{

    string name;
    //Stuff
};

I'm guessing I should have something like:

    this->Crews = new ???;
    this->Vessels = new ???;

in my constructor to initialize the arrays, and attempt to access it with:

this->Crews[0].name = "Ship Number One";

Does this make any sense??? I'd expect the "???"'s to involve a mess of asterisk's, like "*struct (*)Crews" but I have no real idea. I've got normal pointers down and such, but this is a tad over my head... I'd like to access the structures like in the last snippet, but if C++ doesn't like that I could do pointer arithmetic.

I've looked into vectors, but I have an unhealthy obsession with efficiency, and it really pains me how you don't know what's going on behind it.

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays