Tic Tac Toe - bleh, stuck.
But I thought the constructor was the way you initilized variables. If not in the constructor, then where? Just between the class brackets?
You are correct, the constructor is the correct place.
I will give an example that is simpler, because honestly I haven't used c style arrays in this fashion in a while.
In the case of "int m_nVariable = 12;" you are actually initializing a variable on the stack that is "hiding" your classes variable. This is a feature of the language, but usually incurs a warning letting you know that this is happening (depending on your warning settings).
You can still reference your class variable if you have a local variable hiding it, but you need to use your this->m_nVariable to do it. In some cases variable hiding is done by deriving. Base class A has a variable and derived class B has an identical named variable, so to get to the one in class A you scope it A::variable.
In this case you can just get rid of the new variable declaration and init your class variables. You can replace your code with the following and it should work:
I will give an example that is simpler, because honestly I haven't used c style arrays in this fashion in a while.
class ClassName{ ClassName::ClassName (void); int m_nVariable;}ClassName::ClassName (void){ m_nVariable = 12; //Correct way to initialize your variable belonging to ClassName. int m_nVariable = 12; //Incorrect way to initialize your variable belonging to ClassName.}
In the case of "int m_nVariable = 12;" you are actually initializing a variable on the stack that is "hiding" your classes variable. This is a feature of the language, but usually incurs a warning letting you know that this is happening (depending on your warning settings).
You can still reference your class variable if you have a local variable hiding it, but you need to use your this->m_nVariable to do it. In some cases variable hiding is done by deriving. Base class A has a variable and derived class B has an identical named variable, so to get to the one in class A you scope it A::variable.
In this case you can just get rid of the new variable declaration and init your class variables. You can replace your code with the following and it should work:
#include "game.h"Game::Game(){ SquareOne[0] = 5; SquareOne[1] = 217; SquareOne[2] = 5; SquareOne[3] = 147; SquareTwo[0] = 244; SquareTwo[1] = 455; SquareTwo[2] = 5; SquareTwo[3] = 147;... etc running = true;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement