Quote:Original post by Le_Danse_Macabre
the second part about passing a pointer to edit a variable outside the function instead of the temporary one created doesn't. Why not just return the new value and set the variable that way?
Hey,
If you're wanting to pass back several values, there's no current way to do it outside of creating your own structure to return them, or using a boost? tuple, which _will_ eventually become part of the language the way things are going.
But references/pointers are incredibly handy for the following situations:
int GetMousePos(int* x, int* y)
where you want the user to specify either null [0] or an int which receives the mouse x and y pos. Actually defining and copying from a custom structure here is commonly used, but kind of silly IMO, as this is more straightforward, logical and lightweight.
However, if you didn't want to allow null parameters, and want the function to return two values, both of which are important, use references.
int GetMousePos(int& x, int& y)
Everyone [lots of programmers] make a big kerfuffle about pointers and ignore references, but references generally lead to better, cleaner code, because you are guaranteed to be able to view and edit the variables passed, and the compiler won't allow you to do many silly things with references.
Hope this helps,
CJM