You can't construct members in class bodies like that*. You'll have to put that in the constructor.
(*at least not yet, I think the C++11 standard allows it, but you're still doing it wrong because you have a pointer and not an object).
Although, I won't directly answer your question because I suspect you are doing something you shouldn't.
Your 'graphics' object probably shouldn't be owned by the State like that. Since you will have many states, they will all want to share the same Graphics object. If they are each creating their own, that means you will be creating a new window for each state! Not something you want.
Your 'Game' class (or something similar) should own the graphics, then pass a pointer to it to each state. Or maybe instead, it should be a parameter passed to Render, since that's the only time you'll need it anyway.
EDIT:
Looks like I was too slow and Turbine already answered.
Please heed by warning though: you don't want each State to have its own Graphics. That is very weird and will not do what you want.
OK, you're going to have stuff that you want to share between all states, right? Things like game settings (use configurable stuff like controls and whatever), graphics, and maybe some other stuff.
Throw all of that in a struct. Your game should have 1 instance of that struct that it passes around between states.
Your Game or similar class (or I guess main() if you don't have a Game class) would create the graphics and put a pointer to it in the struct. I wouldn't recommend putting the actual graphics object in the struct, just a pointer.
1 2 3 4 5 6 7 8 9 10
int main()
{
Graphics theGraphics( ... whatever ... );
Environment theGameEnvironment;
theGameEnvironment.graphics = &theGraphics;
// now give the environment to your states when they're created
GameState* currentState = new WhateverState( &theGameEnvironment );
}
Your State ctors would then need to take an Environment* as a parameter. You can then access the graphics (and other shared info) through that pointer.
I did that, but it only works in the Constructors. The rest of the functions dont work
That's because GameEnvironment is a parameter. Which makes it local to the constructor.
If you want it to work in the rest of the functions, you'll need to make it available in the rest of the functions =P. Read up on scope rules and member variables.
Basically you need to make a member variable for your states (probably an Environment* whatever), then set that pointer to GameEnvironment in the constructor. Then you can use 'whatever' anywhere in your state class.
sohguanh wrote:
For all pointer related variables, always place a pointer valid check before de-referencing them.
Meh. I don't agree. Unnecessary extra code and overhead.
There you go. Although I would recommend making an Environment* in each state instead of a Graphics*, since you'll probably want to have access to more than just the graphics.
Or if all you need is the graphics, then don't bother with the Environment struct at all anywhere and just pass a graphics pointer (I recommended the Environment struct because I figured you'd want to share other things -- but if not then it doesn't make much sense to have it)