Sign in to follow this  

Set class (member) pointer to a existing class (to allow editing)

This topic is 1778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I have two classes. I am trying to get the 2nd class to have a member pointer that points to an instance of the first class (Giving the 2nd class the ability to edit an instance of the first class) . But I am unsure how to set this up (when I use it the values I try to edit don't change) 

 

Here is what I have tried:

class A
{
 m_MemberA;
};

class B
{
 void SetMemberPointer(*A _a)  {m_MemberB = _a;}
 A* m_MemberB
};

void main()
{
 A Class1;
 B Class2;

 Class2.SetMemberPointer(&Class1);

}

 

Can some one explain why this doesn't work/help with a solution?

 

Thanks 

Edited by AussieSpoon

Share this post


Link to post
Share on other sites

when I use it the values I try to edit don't change

Post the actual code you are using. What you have shouldn't even compile, which means it's very hard to determine why the runtime behavior you are describing would occur.

Share this post


Link to post
Share on other sites

Class 1 (.h):

 

struct DrawArea
{
	float  left, right, top, bottom; 
};

class Texture
{
public:
	Texture(void);
	Texture(IDirect3DTexture9 &_Texture);
	Texture(IDirect3DTexture9 &_Texture, float  _startX, float  _startY, float  _endX, float  _endY);
	~Texture(void);

	IDirect3DTexture9* GetDXTexture() {return m_Texture;}
	DrawArea GetDrawArea()			  {return m_DrawArea;}
	//u = x, v = y co-ordinate
	float GetTextureUMin();	//the x start (left)
	float GetTextureUMax();	//the x end (right)
	float GetTextureVMin();	//the y start (top)
	float GetTextureVMax();	//the y end (bottom)

	uINT GetWidth();		//will return the width of the texture at level 0
	uINT GetWidth(uINT _level);
	uINT GetHeight();		//will return the width of the texture at level 0
	uINT GetHeight(uINT _level);

	void SetToTexture(Texture &_texture);
	void SetToTexture(Texture *_texture);
	void SetDXTexture(IDirect3DTexture9 &_Texture);
	void SetDXTexture(IDirect3DTexture9 *_Texture);
	void SetDXTexture(IDirect3DTexture9 &_Texture, float  _startX, float  _startY, float  _endX, float  _endY);
	void SetDXTexture(IDirect3DTexture9 *_Texture, float  _startX, float  _startY, float  _endX, float  _endY);
	void SetDrawArea(float  _startX, float  _startY, float  _endX, float  _endY);


private:

	IDirect3DTexture9* m_Texture;//The DirectX texture
	DrawArea m_DrawArea;
};

 

 

Class 2 (.h):

 

class Animation
{
public:
	Animation(void);
	Animation(Texture* _Texture, float StartX, float StartY, short Rows, short Columns, float FrameWidth, float FrameHeight, short Speed);
	~Animation(void);

	void Update();

private:

	Texture* m_Texture;//????!?!!?
	float m_StartX;
	float m_StartY;
	short m_Rows;
	short m_Columns;
	short m_CurrentFrame;
	short m_CurrentFrameX;
	short m_CurrentFrameY;
	float m_FrameWidth;
	float m_FrameHeight;
	short m_Time;
	
	//short m_Counter (Might be better to make this global (To only have instance of it instead of 1 per animation)
};

 

Class 2 (.cpp):

 

Animation::Animation(Texture* _Texture, float StartX, float StartY, short Rows, short Columns, float FrameWidth, float FrameHeight, short Speed)
{
	m_Texture = _Texture;

	m_StartX = StartX;
	m_StartY = StartY;
	m_Rows = Rows;
	m_Columns = Columns;
	m_FrameWidth = FrameWidth;
	m_FrameHeight = FrameHeight;
	m_Time = Speed;

	m_CurrentFrame = 0, m_CurrentFrameX = 0, m_CurrentFrameY = 0;
}

 

Creating/using the variables:

 

Animation *TestAnimation;
............

TestAnimation = new Animation(&TestSprite1->GetTexture(), 0, 0, 4, 1, 64, 64, 1);
TestSprite1->SetAnimation(TestAnimation);

Share this post


Link to post
Share on other sites
1) What is TestSprite1? Is it a Texture? It doesn't look like it. In particular what does the GetTexture() function look like?
2) You never actually try to make any changes so of course you don't see any effects.
3) It's easier to do problem solving in these kinds of cases if you can break down the problem to a minimal, but complete, compilable example. To be minimal it shouldn't include anything that isn't relevant to the problem (ex: member functions that are never called) but to be complete it should still show all the bits that show the core of what you are attempting to do.

Share this post


Link to post
Share on other sites

In particular what does the GetTexture() function look like?

 

Oh man, this was my problem. I was returning the texture as an object, but I have a different function for returning it as a pointer. Since the animation take a pointer I needed to return it as a pointer.

 

Thanks heaps for pointing that out, you are a legend.  It works fine now. 

Share this post


Link to post
Share on other sites

//short m_Counter (Might be better to make this global (To only have instance of it instead of 1 per animation)


Unrelated, but in regards to this, I'd suggest making it a static variable in the class, rather than having it clutter the global namespace, or be visible to classes that shouldn't see it.

Share this post


Link to post
Share on other sites

This topic is 1778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this