• Advertisement
Sign in to follow this  

Map Questions

This topic is 1906 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

Hello,

i am currently working on a map for my text game and i ran into somewhat of a problem. In my map class, i have a const int row = 17; and const int column = 17; in the private data members section of the class. so when i go to compile it, i get a string of errors saying i need to have them as static to initialize them in the class header file. My question is why do they need to be static. i don't get the red error line under them when they don't have the static keyword. As i understand it, a static data member is the same for all instances, so does this effect my map if im only going to have one at all times. And if it does effect it, how to i change it to make it work. i have to have the Map array and all the constants in the header file so that the whole class can use them.

Share this post


Link to post
Share on other sites
Advertisement
Yes, you're right, a static member is the same for all instances.
The reason for why they have to be static to be initialized like that? The boring answer; "because the standard says so". I don't know the argumentation behind it, and it has been lifted in the latest C++ version, C++11.
So what you need to do is to initialize your const members in your constructors, using the initializer list.

MyClass() : row(17), column(17) {}

Share this post


Link to post
Share on other sites
Will that allow the whole class to use the values of row and column? because i have a multidimensional array: Map[Row][Column]

Share this post


Link to post
Share on other sites
The way you are using it there is no difference.

Also, it should be noted for completeness sake that
class MyClass
{
SomeType myMemberData = someValue;
};

is valid under C++11, where it is just syntactic sugar for adding :myMemberData(someValue) to every constructor that does not explicitly initialize myMemberData. Edited by BitMaster

Share this post


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

  • Advertisement