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?
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...
}
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.
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
struct ship_base_data{
/*ints, floats, bla bla bla*/
} ship_name_base;
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
No, you don't have an instance of the class called ship. In the case of the 2 objects of the ship class you did declare, you would call like this:
1 2
newship.clear();
newship2.clear();
You can only call member functions of instantiated objects by using the name of the object (or through a pointer to the object which has a slightly different syntax.)
Remember, a class definition isn't an object. It's simply a blueprint that tells the compiler how to build and use an object of that type.
IM NOT USING CLASSES I'm using data structures. This is what I get when I use .clear of an object of type (whatever datat structure):
".clear() not a member os (whetever datastructure)"
It does not work. I'm not using classes.
If it was a class, IT WOULD BE LIKE THIS:
1 2 3
class ship{
BLABLABLA
};
not like this:
1 2 3
struct ship{
BLABLABLA
};
And, @ naraku:
.clear() is not a memebr of my data structure. WHY? because I didn't make it one, because functions can not be part of data structures. That would be what classes are for.
struct ship{
//variables...
};
int main()
{
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.