Map Questions

Started by
2 comments, last by BitMaster 11 years, 5 months ago
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.
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) {}
Will that allow the whole class to use the values of row and column? because i have a multidimensional array: Map[Row][Column]
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.

This topic is closed to new replies.

Advertisement