# initialization of class problem

This topic is 4103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a class, player, that hold the address of another class, party. The thing is that I try to set the reference to NULL by default in the constructor but I get this error: c:\documents and settings\ryan\desktop\my projects\party_test\party_test\player.cpp(8) : error C2354: 'ncharacters::player::_current_party' : initialization of reference member requires a temporary variable Is their a way to set a reference to a class as NULL or something else so that it does not hold something random in it.

##### Share on other sites
Quote:
 Original post by RyanZecIs their a way to set a reference to a class as NULL or something else so that it does not hold something random in it.
Nope, that's the point in references - there's no such thing as a null reference (Well there is, but you shouldn't get one under normal circumstances). If you want to point something to "null", you should either use a pointer, or create a null player that you can test against (They might have an empty name or something)

##### Share on other sites
Cool, I set it to a pointer. Is their something I can set a reference to or should I just make this a pointer since this pointer may or may not point to anything and can change an unlimited amount of times?

##### Share on other sites
References are initilised once, to point at another object. You can't re-assign a reference. If you need to change what the reference points to, you need to use a pointer. That's the reason you don't get null-references; If you had one, you wouldn't be able to change it to point at anything.

If you have:
int a = 42;int b = 100;int& ref = a;

Then doing ref = b; will change the value to the value of b, meaning a will take on the value 100.

##### Share on other sites
Ok, That does make sense to me now that I see it, thanks. Still alittle rusty after take about 1 - 1 1/2 years break from doing any C++ programming but it is now coming back.

1. 1
2. 2
frob
16
3. 3
4. 4
Rutin
11
5. 5

• 13
• 13
• 61
• 14
• 15
• ### Forum Statistics

• Total Topics
632125
• Total Posts
3004250

×