Public Group

# how to change dereferenced variable

This topic is 4832 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 system the includes several hundered lines of code that deal with the map in my creature class, and untill now, I've been handing the map around as a dereferenced argument (map &lvl). well, I finally figured out"hey! wouldn't it be cool if the creature knew what map it was on?" I figured that that wouldn't be too hard to do, because all references to class map in functions of monster are called lvl, so I just made (map &lvl) a part of the creature class, but when I try to pass the reference to the map as an argument to the constructor and set it, it has a problem, specifically: "uninitialized reference member creature::lvl" it says that on the first line of the constructor, before I even get a chance to give it a value(which I do on the very next line) Thanks in advance for your time.

##### Share on other sites
References must be set in the initializer list, not in the body of the constructor. That's because a reference type has no default constructor; it has to be assigned its target as part of construction. Also, once you've constructed the reference you cannot make it point to a new object. Personally, I avoid having references as class members; I find I usually end up needing to change them to pointers.

##### Share on other sites
well, it looks like I have to change everything to deal with a pointer....<grimble> <grumble> Thanks!

##### Share on other sites
No, use an initializer list.

For example:

class B {public: B(map &lvl) :    m_Blah=&lvl, m_Blah2=&lvl {   m_lyk_omg_initialize_me=-345;  }private: map& m_Blah; map& m_Blah2; int m_lyk_omg_initialize_me;}

I'm not sure I got the syntax for the initializer list correctly, but it's something like that.

thanks!

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 12
• 16
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633357
• Total Posts
3011503
• ### Who's Online (See full list)

There are no registered users currently online

×