That's great ta:
Firstly I was just trying to encapsulate the code as it seemed the natural progression if I were creating objects in objects. Is it wrong to try this kind of nesting? Would I be better off just creating a separate class?
I tend to use nested classes in cases where the classes never need to be accessed publicly (i.e. they are only used in private areas of a class). It's just a stylistic thing really, and there's no real problem doing either.
Secondly I was trying to avoid vectors as I thought that you couldn't save them in .bin files. only vars and arrays! Is that a falsehood?
#include <cstdio>
void save(std::FILE* fp, const std::vector<cTroupObj>& objs)
{
uint32_t num = objs.size();
std::fwrite(&num, 1, sizeoff(uint32_t), fp);
if(objs.size()) //< important! (otherwise you'll hit NULL if empty)
{
std::fwrite(&objs[0], objs.size(), sizeof(cTroupObj), fp);
}
}
void load(std::FILE* fp, std::vector<cTroupObj>& objs)
{
uint32_t num = objs.size();
std::fread(&num, 1, sizeoff(uint32_t), fp);
objs.resize(num);
if(objs.size()) //< important! (otherwise you'll hit NULL if empty)
{
std::fread(&objs[0], objs.size(), sizeof(cTroupObj), fp);
}
}