>> Containers can hold standard objects as well as custom objects, as long as the objects in the container meet a few requirements:
The reason for the above is simply so we can to manipulate our ADT's just like native ones:
Code:
struct object
{
virtual ~object() // MUST be virtual
{
}
object() // default constructor
{
data = 0;
}
object(const object & x) // copy constructor
{
*this = x; // see operator =, below
}
const object & operator = (const object & x)
{
data = x.data;
return *this; // so that we can chain assignments together
}
public:
int data;
};
Then you will be able to do:
object a, b, c = a = b;
>> do you have to globally define the struct, or is defining the struct in the same scope you created an instance of the class enough?
It works just the same as with any other data type. You can make it global or better still, pass it as a parameter to the functions that will be manipulating it (but create it in main, of course, so that it doesn't get destroyed prematurally).
The loading of your object might go something like:
Code:
bool loginClass::init(player & p)
{
bool success = false;
ifstream in("player.dat");
if(in.good())
{
in >> p.atk >> p.def >> p.hp;
if(!in.fail()) success = true;
}
return success;
}