I think this works like I a pointer to Tracking. The problem comes because I need to get a variable from Tracking to update the A Class. This varible is stored inside a routine so I can't do like this:
m_tracking->getvarible();
So, any ideas on how to achive this?

Share this post

Link to post

Share on other sites

m_tracking is now a copy of the pointer and points to the object "Tracking".

If you use the pointer to a pointer "B **m_tracking" you might end up into troubles if that pointer gets deallocated or so. One thing I must point out:

//Incorrect

void Tracking(B *Tracking){ m_tracking = &Tracking;}

Is incorrect. In this case the Tracking is a copy of the original pointer and is located in stack. This is bad. m_tracking is now pointing to a memory location in stack which contains the copy of the original pointer. As soon as the function returns, the memory location won't contain anymore a pointer to the original object.You can do this too, but you'll need to have the argument already as pointer to a pointer. Like this:

//Correct

void Tracking(B **Tracking){ m_tracking = Tracking;}

To create a copy of the original object you can do like this:

B m_tracking; //m_tracking isn't a pointer

void Tracking(B *Tracking){ m_tracking = *Tracking;}

Either way you do it (preferably the first way) :

m_tracking->getvarible(); //(the first way)

or

(*m_tracking)->getvarible(); //(the second way)

isn't a problem.

Best regards

0

Share this post

Link to post

Share on other sites

Since you have a pointer to a pointer, you will need to dereference it twice before you can call any functions on the object. The '->' operator will do this for the current pointer but not for multiple levels if indirection, you can use the dereference pointer operator '*' to do this yourself

Share this post

Link to post

Share on other sites

Thanks guys, I knew the first method was correct, I've done it a million times. did a check of both variables in the first method and It was exactly as I wanted it? I thought is was right, and i was. The problem was that in the get variable method of the B function I had a problem that was not passing the correct variable to the A function so It always came back as the same value, just a minor programming error. I'm really sorry!!! my bad.Anyway, now it works and Thank you for letting me know I was on the right track.

0

Share this post

Link to post

Share on other sites

This topic is 4200 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.