Skip to main content
gamedev.net
🔒 Locked

Untitled

Started by endasil 1 reply 900+ views
Original Post
endasil
endasil
It seems like i do not fully understand how pointers works. I would be very thankfull if someone would explain to me what i am missing. { ---> Okay NodeReceiver here is the pointer that i am interested in. ---> When i send it to the function checkForNodeEdgeAt, i was expecting ---> the value it received in there to remain when it got out of the function. CNode *NodeReceiver = NULL; int iEdgeNrReceived = 0; ---> Call to function that changes NodeReceiver CNodeMgr::getSingleton().checkForNodeEdgeAt( miMouseX , miMouseY , NodeReceiver , iEdgeNrReceived); } ---> The function that is beeing called void CNodeMgr::checkForNodeEdgeAt(int iXPos, int iYPos, CNode* NodeReceiver, int &iEdgeNrReceiver) { map<unsigned int,CNode*> ::iterator next = mNodes.begin(); while(next != mNodes.end()) { iEdgeNrReceiver = next->second->checkForNodeEdgeAt(iXPos, iYPos); if(iEdgeNrReceiver != -1) { NodeReceiver = next->second ---> Using the debugger and stepping trough the code ---> i can see that the NodeReceiver now looks like this: ---> "+ NodeReceiver 0x003d5d30 {miNodeID=3 miX=300 miY=400 ...} CNode *" break; } next++; } } Once it gets back to the calling function, it reverts to: + NodeReceiver 0x00000000 {miNodeID=??? miX=??? miY=??? ...} CNode * How come?
EndasilVisit my site, try my gameswww.dragonrealmsoftware.comI need more beta tester for my game! Check out http://www.dragonrealmsoftware.com
TDragon
TDragon
You're only modifying the value of the pointer variable passed to the function, rather than the value that the variable is pointing at. To make it keep the value, pass by reference:
void CNodeMgr::checkForNodeEdgeAt(int iXPos, int iYPos, CNode*& NodeReceiver, int &iEdgeNrReceiver){...

(YMMV)
JohnBolton
JohnBolton
Pointers get passed to functions just like other variable types. For example,
        char const * p = NULL;        int          i = 0;                ByValue( p, i );                // at this point p and i are both unchanged                ByReferenceReference( p, i );                // at this point, p points to "changed by ByReferenceReference" and i = 2                ByReferencePointer( &p, &i );                // at this point, p points to "changed by ByPointerReference" and i = 3            ...            void ByValue( char const * x, int y )    {        x = "changed by ByValue";        y = 1;    }    void ByReferenceReference( char const * & x, int & y )    {        x = "changed by ByReferenceReference";        y = 2;    }    void ByPointerReference( char const * * x, int * y )    {        *x = "changed by ByValue";        *y = 3;    } 
Look at the parameters and their types in each function. See the differences?
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

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

Sign in to reply to this topic.