Skip to main content
gamedev.net
🔒 Locked

Pointer to pointer class

Started by mvtapia Jun 14, 2006 at 2:51 AM 2 replies 500+ views
Original Post
mvtapia
mvtapia
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?
Marco Tapia
Demus79
Demus79
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:

//Incorrectvoid 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:

//Correctvoid 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
RdF
RdF
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 = &somecalsspointpoint = & point;point->CallSomeFunc(); (*point).CallSomeFunc();(*pointpoint)->CallSomeFunc();
mvtapia
mvtapia
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.
Marco Tapia

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.