Jump to content

  • Log In with Google      Sign In   
  • Create Account


Map Questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Crusable   Members   -  Reputation: 590

Like
0Likes
Like

Posted 05 November 2012 - 12:30 AM

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.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


Sponsor:

#2 patrrr   Members   -  Reputation: 867

Like
1Likes
Like

Posted 05 November 2012 - 01:24 AM

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) {}

#3 Crusable   Members   -  Reputation: 590

Like
0Likes
Like

Posted 05 November 2012 - 01:31 AM

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

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#4 BitMaster   Crossbones+   -  Reputation: 3006

Like
0Likes
Like

Posted 05 November 2012 - 02:14 AM

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, 05 November 2012 - 02:19 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS