static variable won't iterate within class func

I just created a static variable within the function defination of one of my class members of my header file.

I tested it with cout << pos_offset
to see if there is an iteration going on. Sadly the static variable doesn't iterate - it always stays 1.

Are there specific rules for class member functions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 void Screen_output::field()
{
	
	char * array = file_to_array();
	
	static int pos_offset = 0;
	pos_offset =+ 1;
	
	cout << pos_offset;
	
	array[939+pos_offset] = 'X';	//create Player Spot X
	
	for (int i = 0; i < field_size-1; i++)
	{
		cout << array[i];
	}
}
You have your operator the wrong way round: Each time you are setting it to +1: pos_offset = +1;
Try changing it to this instead: pos_offset += 1;

Also, the danger with your function there is you perform no bounds checking on pos_offset, so it is easy for a segmentation fault to occur, and if you have multiple instances of Screen_output pos_offset will have the same value between them, which could be problematic (based on what you are trying to do, of course).
Oh man what a trivial one again :D

Well I didn't used += that much so I might be spoken not guilty :P

thx bro
Just a thought (1 cent worth):

If only incrementing by 1, then just use ++pos_offset;

I guess the optimiser would do that anyway.

Have a good week end, all :+)
Topic archived. No new replies allowed.