I know that with classes you have deconstructors, but is there anything I can use for data structures?
1 2 3 4 5 6 7 8 9 10
double experience = 0;
long damage = 0;
long health = 100;
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?
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";
// the vector of levels will have to be completely re-declared, as each level has different attributes/etc...
//now mabey some calculations or whatever
//new ship?? alright, lets delete the old one now...
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.
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.
So, how could I allocate/delete the memory for these structures?
I want it to be as efficient as possible.
also, I want to add another question:
What if I have this structure:
1 2 3
/*ints, floats, bla bla bla*/
I want to call the instance without creating a new one (use the same memory space, so as to not take up more memory), so (I'm trying to confirm what I know... please correct me if im wrong) I would call it like this:
I know that wouldn't be practical, but for other purposes (like program mechanics, such as a bool that stores whether the program is on it's first ever run) I would like to know if this is feasable, so I don't have to pass the structures from function-to-function.
and why are we using ship to declare myship with? I thought we had to use the structure's handle:
I'm not sure what you mean with handle (english is not my native language), but your struct is called 'ship', so I declared a variable of type 'ship*'. If it was called 'ship_data' then your declaration would be correct
myship points to the address of ship
Not exactly. It point to an instance of the class 'ship' (or "an object of type 'ship'")
so if we modified myship, we would modify ship as well?
If you modify a pointer it means you're making it point to another object. If you mean in the case of using it as argument for a function call then yes, modifying the object pointed by it will modify the same object that exists in the calling function.
I guess this is a bit confusing. Unfortunately to use dynamic memory you have to use pointers, and I'm not probably good enough to explain them properly
ship newship, newship2;
//does "ship.clear()" reset both of them?
If clear() isn't a member of ship then why are you using the member operator? To use a non-member function on an object you'll have to pass that object to the function.
Besides, what I said about being a blueprint was for objects of any type, not just classes. Structures operate under the same principal. Even once a struct is defined you have to instantiate it by declaring it like a variable, IE typename objectname;. Trying to access any part of a user-defined data type before you create an object of that type won't work.