I'm still confused as to what exactly &getCharacter and SetCharacter(Character &c) are doing. It's one of the things I read about but still don't quite understand.
One last edit should make things clear, since I didn't take into account that we need a way to properly retrieve the Character object being set.
class Character
{
private:
Item *items; //resembles a character capable of having many items
//private stats variables...
public:
Character(/*code for stats...*/){/*constructor code...*/};
void setItems(Item *theItems); //sets the items
Item getItem(); //returns a copy of the item, not a reference
void selectItem(int n); //selects the nth item from the list
void useItem(); //use the currently selected item
void selAndUse(int n); //selects an item and immediately uses it
};
//Chances are the above code would be applicable mainly for useable items--
struct Item //most likely a Useable item
{
private:
Character *invoker; //a pointer, EDIT
public:
Item();
virtual void use() = 0; //pure virtual function, no general Item can be created
//unless it extends this class and overrides this function
void setCharacter(Character &c); //sets the character to access when use is invoked
protected:
Character &getCharacter();//returns a reference to the character, but only for derived Items //EDIT
};
//more code...
This is going to be fairly difficult to explain, so please bare with me.
When you create an item, you want it to have some kind of Character to associate with when it is used.
This concept is fine, but when we do this--
void setCharacter(Character &c)
--its because I want a reference to an actual character object, and I don't want the object passed as an argument to be a copy.
The reference symbol means that we will be passing the actual character and can change/alter data or even assign a pointer to the address of the passed Character object to use later.
For a clearer example, consider the following code--
void sum(int a, int b)
{
a = a + b;
}
int main()
{
int x = 4, y = 5;
sum(x, y);
cout << x << " " << y << endl;
cin.get();
return 0;
}
Notice that x and y remain 4 and 5 despite the fact that I stated-- a = a + b;
--in the method. x should be 9 correct?
The answer is no, because a copy of variable x was sent to the method, not the actual reference of x.
Let's say that I want to make a change to the variable by using the method. I would have to make a minor change--
void sum(int &a, int &b)
{
a = a + b;
}
int main()
{
int x = 4, y = 5;
sum(x, y);
cout << x << " " << y << endl;
cin.get();
return 0;
}
--Now x is 9 and y is 5, because the actual variable x was passed as an argument, not a copy of x.
The main reason why I wanted to pass the actual reference of the Character instead of a copy, is somewhat for the same reason but I goofed up slightly. Notice that I made Character *invoker a pointer. The reason is for the following-- void setCharacter(Character &c)
{
invoker = &c; //now invoker pointer points to the Character reference c
}
--and now we should be able to access the actual Character that was sent as an argument, and not a copy.
If we sent a copy, any changes we would do to the value pointed to by invoker would be for the copy and not the actual Character.
Edit: Had I left invoker a non-pointer, the assignment-- invoker = c;
--would have sent invoker a copy of the current state of the Character &c, but even so it would still be a copy and not the actual reference that I want control of.