Jump to content
  • Advertisement
Sign in to follow this  
NIm

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!