0

in my code i have this:

void f1(Obj*x){
if(x==0){return;}
...
}

void f2(Obj&x){
...
f1(&x);
...
}

void f3(Obj*x){
...
f2(*x);
...
delete x;
}

i do not like that I pass a pointer to f3, then take it value to pass it as reference, then to take the address to pass it as pointer.

it is better if f2 take a pointer too?

  • 2
    Or perhaps it's better for the others to take references. Though worse is that `delete` statement, it should be wrapped up. – GManNickG Jan 28 '11 at 13:46
  • Can you not pass all by reference? – Tony The Lion Jan 28 '11 at 13:47
  • f2 has a contract that it should never take a NULL so a reference is fine there. It might modify the object so it takes non-const. f1 allows you to pass a NULL and handles the case. – CashCow Jan 28 '11 at 13:58
  • In the example `f3` and `f1` should be taking a pointer, since the former takes the ownership of `x` and the latter accepts `NULL`. – peoro Jan 28 '11 at 14:39

3 Answers3

0

Use a simple design for the interface of your class. In particular, prefer references to pointers.

Use pointers as parameters only when there is no alternative. In these cases, document exactly who owns the pointer (typically, the caller - hence, typically, you should not delete pointers passed to you).

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
0

The normal rule would be that if you can take a reference then you do. With the case of f3 in your example though, the fact it deletes the pointer suggests it is much simpler if your function takes a pointer, as nobody would expect a function to delete a reference.

f2 forces the user to pass in a non-NULL so a reference is a good idea. Presumably const-correct without any further information, i.e. f2 may modify the Obj by calling a non-const method on it.

It is difficult to see whether the design is a good one with names like f1, f2 and f3. Sometimes the names can make all the difference. For example, if the name of f3 is "useAndDispose" or similar it is clear to the caller of the function that the pointer will get deleted after use.

Similarly if your function returns a resource that the user must clean-up, a name that indicates this (createSomething rather than getSomething) is usually preferable.

Daniel said "typically" you do not delete pointers passed to you. Not "never" though. The function may specifically be a clean-up function or it could be a workflow situation whereby something is passed around and then disposed of.

I would say though that you should never delete references passed to you (taking their address first, of course) because nobody looking at your code would ever expect it to do that.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • +1, it's nice to emphasize about function names and semantics. (Erm, sadly I already reached the daily vote limit today, will +1 tomorrow `;-)` ) – peoro Jan 28 '11 at 14:43
0

The main difference between a pointer and a reference is that a reference can not be NULL. So, if the parameter to f2() is mandatory then you should pass by reference.

Check out this other questions.

Community
  • 1
  • 1
vz0
  • 32,345
  • 7
  • 44
  • 77