Sign in to follow this  

Pointer to pointer class

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.

Recommended Posts

Hello everyone, I have a class A and a class B now class A has a variable that is supose to point to a class B object. If I do something like
Class A{
void Tracking(B *Tracking)
{
    m_tracking = Tracking;
}

B *m_tracking;
}

Is the pointer m_tracking a copy of Tracking or is a reference to Tracking. Now, If I change like this
Class A{
void Tracking(B *Tracking)
{
    m_tracking = &Tracking;
}

B **m_tracking;
}

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
The first class with "B *m_tracking;" Is correct in my opinion.

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

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


Foo someclass;
Foo * point;
Foo ** pointpoint;
point = &somecalss;
pointpoint = & point;

point->CallSomeFunc();
(*point).CallSomeFunc();
(*pointpoint)->CallSomeFunc();


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.

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.

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