confused by reference and deference

I can't understand which i should use where, mainly because my brain is fried right now.

Basically i have a class called "house" and a class called "player", and each instance of "house" has a variable called "owner" which is of type "player".

I'm used to programming in VB.NET so pointers and references are new to me. I want the variable "owner" to contain a reference to an already existing instance of player, and not have it create one of it's own.

How should i do this? Would it even do what i don't want it to do anyway?

and i may have answered my own question but can someone please clarify for me? i'm just getting more confused

cheers :)
I'm used to programming in VB.NET so pointers and references are new to me. I want the variable "owner" to contain a reference to an already existing instance of player, and not have it create one of it's own.

Then you should use a reference or a pointer. References can't be made to refer to anything else once they're initialized, so use a pointer if the owner can change during the object's lifetime.
So am i right thinking it should read like this:

1
2
3
4
5
class House
{
public:
Player* owner;
};

Yes, that would be a pointer to Player. You need to take care that the owner cannot be destroyed before the house (or make sure the house is notified in some way) in order to avoid having a stale pointer.
Last edited on
thank you very much :) brain is not working today
Topic archived. No new replies allowed.