Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a modifiable parameter to c++ function

Provided, I want to pass a modifiable parameter to a function, what should I choose: to pass it by pointer or to pass it by reference?

  1. bool GetFoo ( Foo& whereToPlaceResult );
  2. bool GetFoo ( Foo* whereToPlaceResult );

I am asking this because I always considered it the best practice to pass parameter by reference (1), but after examining some local code database, I came to a conclusion, that the most common way is (2). Moreover, the man himself (Bjarne Stroustrup) recommends using (2). What are the [dis]advantages of (1) and (2), or is it just a matter of personal taste?

like image 330
SadSido Avatar asked Aug 24 '09 13:08

SadSido


People also ask

Can you change value of parameter in C?

C Parameter passing. By-Value: The value of a variable is sent to function. The actual parameter cannot be changed by function.

Can you pass a function as a parameter in C?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer.

What is parameter passing technique in C?

Parameter passing involves passing input parameters into a module (a function in C and a function and procedure in Pascal) and receiving output parameters back from the module. For example a quadratic equation module requires three parameters to be passed to it, these would be a, b and c.

Does pass by reference allow modification of a function argument?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in.


1 Answers

I prefer a reference instead of a pointer when:

  • It can't be null
  • It can't be changed (to point to something else)
  • It mustn't be deleted (by whoever receives the pointer)

Some people say though that the difference between a reference and a const reference is too subtle for many people, and is invisible in the code which calls the method (i.e., if you read the calling code which passes a parameter by reference, you can't see whether it's a const or a non-const reference), and that therefore you should make it a pointer (to make it explicit in the calling code that you're giving away the address of your variable, and that therefore the value of your variable may be altered by the callee).

I personally prefer a reference, for the following reason:

  1. I think that a routine should know what subroutine it's calling
  2. A subroutine shouldn't assume anything about what routine it's being called from.

[1.] implies that making the mutability visible to the caller doesn't matter much, because the caller should already (by other means) understand what the subroutine does (including the fact that it will modify the parameter).

[2.] implies that if it's a pointer then the subroutine should handle the possibility of the parameter's being a null pointer, which may be extra and IMO useless code.

Furthermore, whenever I see a pointer I think, "who's going to delete this, and when?", so whenever/wherever ownership/lifetime/deletion isn't an issue I prefer to use a reference.

For what it's worth I'm in the habit of writing const-correct code: so if I declare that a method has a non-const reference parameter, the fact that it's non-const is significant. If people weren't writing const-correct code then maybe it would be harder to tell whether a parameter will be modified in a subroutine, and the argument for another mechanism (e.g. a pointer instead of a reference) would be a bit stronger.

like image 200
ChrisW Avatar answered Sep 18 '22 20:09

ChrisW