Jump to content
  • Advertisement
Sign in to follow this  
kpanghmc

Question about passing pointers as parameters in C++

This topic is 4508 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

Hi, just had a quick question. Let's say I have a class called Poop and I want to write a function that sets the poop pointer to NULL. Which one of these would be the correct way to do it or would they both work? My main concern is that in the first version, a local copy of the poop pointer would be set to NULL and the actual pointer passed into the function would be unaffected (similar to when you pass variables in by value instead of by reference). void setPoopToNull(Poop* poop) { poop = NULL; } void setPoopToNull(Poop** poop) { *poop = NULL; }

Share this post


Link to post
Share on other sites
Advertisement
You;re right. You have to either pass a pointer to something you want to change or (better yet) a reference.

void
setPoopToNull(Poop*& poop)
{
poop = NULL;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
You;re right. You have to either pass a pointer to something you want to change or (better yet) a reference.


To expand, pointers, just like any other object, are passed by value by default. If you don't want to work with a copy of the pointer, a reference is needed.

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!