C++ (pointer/callback) trouble

Started by
2 comments, last by Qpeg 20 years, 6 months ago
I do not consider myself an expert with c++, and especially I am not fond of pointers, although there is really no other way to go so I am forced to use them. In my current project (a game engine) I have encountered a weird problem; I have made lots of different wrappers for D3D, DirectInput etc. in the form of classes that initialize, query, hold variables and so forth. This is all pretty standard stuff, nothing fancy, but lately I have stumbled over a problem. _Some_ of the variables in my classes do not seem to "keep" their new values when I change them.. In a class for managing players in the game, I keep a vector containing all the players, in addition to a pointer to the current player. In a member function called SetCurrentPlayer(SiegePlayer *player) I try to update this pointer. I do some debug output while doing this to see what happens: First I check what the original currentplayer was, which turns out is correct. Then I update the pointer, and then check what the pointer is. Now the pointer is updated and correct. When the function has exited, I query the manager what the new value is (again, sort of) but this time it returns the player I originally initialized the manager with! I first encountered this type of problem while making a Direct Input wrapper where I wanted to store the current x and y pos of the mouse in the wrapper object. The same thing happened, the update function appeared to work fine, but when I queried the object for the outside, they were back to their original values! I managed to omit this problem by *sight* storing the values as variables outside in the calling class. This is not working with the manager, possibly because I call the manager from a callback function. I can''t think of much more info to pass to you other than this is C++ with DX8, written in VS.NET. There is nothing fancy in my code, so posting it all here would be a waste. If anyone really has an idea about what might be wrong, but has to look at some code, I could email some of it by request. I have had a friend who is pretty good at C++ have a look at it, but he has never seen anything like this. This is really freaking me out, there is little hair left on my head ;P
Real programmers don't comment their code, it was hard to write, it should be hard to understand
Advertisement
post the function in particular, and maybe the class definition.

I think I know what''s wrong, but I need to see it.
quote:Original post by Qpeg
_Some_ of the variables in my classes do not seem to "keep" their new values when I change them..

That means you are changing something else, or something else is changing it back.

quote:
In a class for managing players in the game, I keep a vector containing all the players, in addition to a pointer to the current player.

Do you add players to the vector after taking a pointer to the current player? Adding stuff to a vector invalidates iterators, which includes pointers to data elements.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
this last thing with the pointer to the current player is probably the root of the problem, i did a quick test but it didn''t seem to resolve tho. Its getting pretty late here in norway now, so I''ll have to sleep on it. Thanks a lot tho!
Real programmers don't comment their code, it was hard to write, it should be hard to understand

This topic is closed to new replies.

Advertisement