Friday, July 31, 2009

C++: When do you decide to pass variables by value, reference, or pointers?

Two situations:


1) When we want to modify the actual variable passed as the function parameter


void swap(int %26amp; a, int %26amp; b) ==%26gt; Swaps values of a and b.


2) If we do not want to make copy of the parameter for example


void display (const int%26amp; n) // pass by reference.


Here you do not want to change the variable n , also you do not want to create a local copy (if u pass by value a local copy will be created).

C++: When do you decide to pass variables by value, reference, or pointers?
It is purely based on requirements %26amp; operations
Reply:Pass by value if it's small, for example integers or characters or pointers. Pass by reference if you want to save a memory copy and the value you pass should not be null. Passing by reference is similar to pointers, in that only a pointer sized piece of memory will be transmitted, however it's understood that passing by reference implies the object is not null. Pass by pointer if you want to save on memory copy and the value could be null (in which case null might be a special sentinel value condition).
Reply:it's a tricky question, and you need some experience to decide what to do.





by value: i always pass by value, unless i'm passing an object. if you pass by value, the value is copied; if you're passing an object (i.e. std::string or your own objects) by value, the copying can cost performance, and therefore i want to avoid it.





by pointer: that's what i use if the original value should be modified. You can also use references in that case, but there's one difference: a pointer can be NULL, a reference cannot be NULL. sometimes i use this feature to signal that this variable can be optional (by setting it to 0).





reference: i don't like references, therefore i don't use them a lot. but i do like const references. they are useful, especially if you want to avoid copying objects.
Reply:First you have to choose pass by reference if the


function or method is permitted to (and is capable of) modifying the actual argument. This implies that the argument must be an expression (lvalue) that


can reasonably be modified. Example : swap function.


But if you are using the value of the argument to compute a new value or do something else, it's cleaner to pass by value, except if there is performance issue such as the following.


Here is a recommendation by Meyers (Effective C++) :


prefer pass-by-reference over pass by value, except for built-in types and STL iterator and function object types.


So if your copies are lightweight enough , pass-by-value but if you are passing something large like an struct, choose pass-by-reference or pointer .


choosing between pointers and references is more of a style preference as there is no difference in performance .pointers are more explicit than references, if we do not go with pass-by-value. So they make a cleaner interface.


No comments:

Post a Comment