Deleting data in Data Structures

IWishIKnew (460)
I know that with classes you have deconstructors, but is there anything I can use for data structures?

EX:

1
2
3
4
5
6
7
8
9
10
struct level{
    double experience = 0;
    long damage = 0;
    long health = 100;
};

struct ship{
    vector<level> levels = vector<level>();
    string ship_name = "NO_ID";
};


lets call this a small psuedo structure. If I used the same structure address (in other words, I never declare a new instance, I only use 1), and I want to delete all the data in this structure, is there any way other than writeing a function to clear it all/declare defaults?

ex code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void load_everything(ship& player_ship)
{
    /*load load load...*/
}

void clear_ship_data(ship& player_ship)
{
    /*Just basically calling all the members of ship
    and setting their default values...*/
    player_ship.ship_name = "NO_ID";
    player_ship.levels.clear();
    player_ship.levels = vector<levels>();
}

main()
{
    ship player_ship;
    load_everything(player_ship);
    //now mabey some calculations or whatever
    //new ship??  alright, lets delete the old one now...
    clear_ship_data(player_ship);
    return 0;
}


don't focus on the code errors in that, I pretty much just wrote that on the spot just now to give you an idea of what I'm currently doing to clear data structures.
Last edited on
naraku9333 (919)
In c++ structs have constructors and destructors too, but the dtor wont be called until the variable goes out of scope or if you call delete on it (if it was allocated with new). For what your doing just reseting the variables to defaults looks like it will make the most sense. I also want to note you shouldn't do this player_ship.levels = vector<levels>(); clearing the vector should be sufficient, your adding unnecessary overhead with ctor and operator= calls.
Last edited on
Registered users can post here. Sign in or register to post.