Pointers to a class that is part of a list, problems

Started by
7 comments, last by yewbie 14 years, 11 months ago
Ok, this might be a hard to explain and its pretty complicated for me so I will try to explain what I am doing... I am creating a semi dynamic animation system to store animation "events" in a list the list is made up of a class, and in that class I have a pointer pointing back to another class in another list but when I try to run it all the information in the class im pointing to comes up invalid... I am probably not explaining this very well so here is some source code of what I am doing... If I am doing this correctly PlayerObject *Player Creates a pointer to another set of data contained in another list, im sure this is just something dumb I am doing and not realizing how this is supposed to work, like maybe creating a new instance and not initializing with data? I have been working on this all day now, im so very confused as to why this won't work. Pretty much what I want to do it use PlayerAnimation->Player->blah to access the stuff in player, I really appreciate any help anyone can give

class PlayerAnimation
{
	private:
		
	public:

		int ID; //id of the player for whom this animation belongs
		int StartAnimation; //this is the animation we return to when we are done
		int TilesToAnimate; //number of tiles in total to animation
		int TilesAnimatedSoFar;
		int xvelocity;	//veclocity in pixels per frame x direction
		int yvelocity;	//veclocity in pixels per frame y direction
		DWORD AnimationSpeed; //speed in milliseconds for next animation update
		DWORD LastAnimation; //the tick count for our last update
		PlayerObject *Player;

		PlayerAnimation(int myID,int TilesToAnim, int xvel, int yvel, DWORD AnimSpeed, PlayerObject *play) //constructor
		{
			ID = myID;
			StartAnimation=play->CurrentAnim; //grab our current animation so we can return to it after we are done
			TilesAnimatedSoFar=0;
			TilesToAnimate = TilesToAnim;
			xvelocity = xvel;
			yvelocity = yvel;
			AnimationSpeed = AnimSpeed;
			LastAnimation = 0;
			Player = play;
		}
		~PlayerAnimation() //deconstructor
		{
			

		}
};


Advertisement
Here is how im sending the pointer to my Player list to this class

PlayerObjectList::iterator listiter;
listiter = MyPlayers.begin ();

PlayerObject * player = *listiter;

and I send it to the class like so

MyPlayerAnimationList.push_back(new PlayerAnimation(ID,8, 0, 8, 100, player ));

I don't think there's enough information there for us to do any more than guess at what the problem might be. Ideally, you should post a minimal working example that demonstrates the behavior in question, along with a detailed description of the actual problem you're seeing (including the exact text of any error messages you're receiving).

Reducing a large and/or complicated program to a minimal working example can be tedious, but sometimes it's the most straightforward way to track down bugs like this. If you're not able to do that, at least post some more code. In particular, it would help to see the declarations of some of the variables in your example so that we know their types.

Also, a few minor suggestions:

1. Look into 'initializer lists'.

2. Don't define empty non-virtual destructors; the compiler-generated destructor will suffice.

3. The name PlayerObject is somewhat redundant; how about just 'Player'?
Yeah, I think im just going to build a whole new project just with those 2 classes checking how I can get them to work together...

I thought it would be as simple as declaring a pointer to class A inside of class B and then passing the reference, but either im not doing that correctly, or I'm not supposed to do that =p
Quote:...or I'm not supposed to do that =p
No, you can do that :) Although it's not always the best design decision, it's perfectly valid (and quite common) to have object B store a reference of some sort to object A.
Well a wrote a whole thing testing it and..... annndd it worked fine :(, use the same classes etc...

And now it only crashes out like it was in debug mode saying that my reference to the other object has no data in it... even though if I don't run it in debug in the compiler it works fine...

One question though, if I don't specify a deconstructer and have a pointer to another object as part of my class will the default desconstructor release that object as well? or just release the pointer?
From what you have there you aren't doing any smart pointer tricks, so basically as long as you don't call delete explicitly nothing will be released. In your case when the PlayerAnimation destructor is called the only the variable PlayerObject which you have within that class is out of scope. The memory address and information in there is still valid

General design rule with raw pointers/or passed in objects the way you are doing it - if the class did not allocate the memory for your player object and was passed in from outside, that class should not destroy the memory for the object.
The more applications I write, more I find out how less I know
Thanks for the reply, yeah I can't seem to figure it out... Maybe its some weird problem with the debugger, ah well I tested it in release mode and it works fine all the data is readable etc
Well it was actually a iteration problem, I was deleting my object and restarting the iterator but the iterator was invalid, but it ran fine in release mode which was very strange... took me like 4 days to figure it out but I just had to iterate at the bottom of my for loop only if iteration is continuing, works like a charm!

Tested my player animation with 200+ objects all moving randomly, seemed to work great.

This topic is closed to new replies.

Advertisement