A problem of Scope, i think...

The attached is from a project i'm working on using the D2D DemoApp from the MSDN website. I'm trying to create a moving object where the object is instantiated from it's own class (a car, a monster, whatever) The problem seems to be entirely in the app class at the moment though. Attached is just the results from the log file the program is generating and the two calls where the variables are used.

Log - first from the constructor, then from the render()
from constructor ...
200 by 200
300 by 300
from render ... 
-858993460 by -858993460
-858993460 by -858993460

In the constructor I call this. The variables named are all public as I was trying to focus on getting the program to render a moving object and didn’t want to create a bunch of getters and setters.
1
2
3
4
5
6
7
8
9
	int pos[4] = { 300, 300, 300, 300};
	int xposM = 200;
	int yposM = 200;

	log2.open("logfile2.txt", ios::app);
	log2 << "from constructor ..." << endl;
	log2 << xposM << " by " << yposM << endl;
	log2 << pos[0] << " by " << pos[1] << endl;
	log2.close();


In the rendering function I call this
1
2
3
4
5
log2.open("logfile2.txt", ios::app);
	log2 << "from render ... " << endl;
	log2 << xposM << " by " << yposM << endl;
log2 << pos[0] << " by " << pos[1] << endl;
	log2.close();


Any thoughts?
int xposM = 200; is a local variable, that dies when the constructor finalizes.
It is not your member property, but it is hiding it.
But it's initializing an object so shouldn't the object retain the data? How do i fix that?
just xposM = 200; or this->xposM = 200; if you prefer.

Your issue is similar to
1
2
3
4
5
int x; //global
void foo(){
   int x; //local
   x = 42; //modifying local
}
ahhhhhh the constructor didn't need to restate the type. Wow, that should've been obvious.

Thanks!!!
Topic archived. No new replies allowed.