Jump to content
  • Advertisement
Sign in to follow this  
fooman_69

pointer trouble

This topic is 4763 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 a problem passing my pointers around. I have a pointer
Entity *player1;
Now I have to pass it to a function from the Video class with a signature such as...
InitVideo(Entity *player1)
Now, this function passes it to a private function in the Video class that generates a random map with a signature that looks like this
GenMap(Entity *player1)
There the pointer will be set
player1 = new Entity(0, 100, 0, 100)
Now when I call the Render function from within my main I see in my debugger that the player1 pointer has not actually been set, and my program crashes. Can someone help me pass the pointer around properly??

Share this post


Link to post
Share on other sites
Advertisement
When you pass a value to a function, you pass a copy. The reason pointers work is because you are passing the location of an object. Think about it: to change the value of a primitive pointer, you have to dereference it. With classes, you have to call public methods.

Here is a basic example of what you were trying to do:


void method(int b)
{
b = 5;
}

int main(){
int a = 4;
method(a);
//what is a here?
return 1;
}






Would you expect a to be 5, after the function? of course not: you are passing a COPY of the value. Just as your are passing a COPY of the pointer in your code. Somewhere in memory is location a, which stores the pointer info. When you call your method, a new location is created in memory, location b, which has a copy of location a's data. When you change b in your method, a stays the same , right? But if you dereference b, it would point to the same place a would point to. But you can't actually change where a points, because it is a copy. Therefore, you could pass in a double pointer (**) and dereference the first pointer to change the value of a.


void someMethod(Entity **a)
{
*a = new Entity(...);
}



Or what you could do is return the new pointer value, and set player equal to that.

Share this post


Link to post
Share on other sites
When you pass in the pointer to your function you're creating a copy of the pointer so when you say "player1 = new Entity(blah blah)" you're not actually modifying the pointer you passed in. One way to fix this would be to do:

InitVideo(Entity** player1)

GenMap(Entity** player1)


And say from within one of these functions
*player1 = new Entity(0, 100, 0, 100)

Share this post


Link to post
Share on other sites
Consider this code:
void GenMap( int z )
{
z = 5;
}
void InitVideo( int y )
{
GenMap( y );
}
void foo()
{
int x = 0;
InitVideo( x );
}


Now, do you expect the values of x and y to change because you changed the value of z? No, and you would expect the same for any type include Entity*.

Now take a look at the following code (the key differences are bold:
void GenMap( Entity ** player1 )
{
*player1 = new Sprite(0, 100, 0, 100);
}
void InitVideo( Entity ** player1 )
{
GenMap( pPlayer1 );
}
void foo()
{
Entity * player1
InitVideo( &player1 );
}

Share this post


Link to post
Share on other sites
ok, but when I code
player1 = new Entity();
I can't do that if the player1 is passed by reference. Must I override the operator?

EDIT* didn't see the 5 new posts...!

Share this post


Link to post
Share on other sites
Last question...
Do I dereference the pointer to use any of the functions available within the class it is pointing to?

ex. Is it right to code
*player1->GetCoords()

Share this post


Link to post
Share on other sites
Quote:
Original post by fooman_69
Last question...
Do I dereference the pointer to use any of the functions available within the class it is pointing to?

ex. Is it right to code
*player1->GetCoords()


Almost. It should be

(*player1)->GetCoords()


assuming the above line is supposed to be inside one of your functions.

EDIT: EDIT OF EDIT: Nevermind

Share this post


Link to post
Share on other sites
When you pass the double pointer, you are passing the location of the pointer, right? So when you dereference once, it points to the value of the pointer (as in, the location the pointer is pointing to). If you dereference twice, it points to the object the pointer is pointing to. So you will want to use a dereference and ->

So lets say we have this:

void method(Entity **player)
{
//this sets the pointer passed in to point to this new entity
*player = new Entity(...);

//dereference, then use dereference -> to call method
(*player)->EntityMethod();
}




EDIT: Whoops, beaten to it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!