0

Possible Duplicate:
When pass-by-pointer is preferred to pass-by-reference in C++?

Hello everyone,

What do you consider a better programming practice: passing objects as pointers or references to functions.
What do you do for input validation?

Thanks.

Community
  • 1
  • 1
Andrew
  • 2,309
  • 4
  • 27
  • 42

2 Answers2

4

It is better C++ style to use a reference. One advantage to this, as I believe you were implying, is that when passing by reference, you no longer need to verify that it is non-null, since references cannot be null. Also, I should add that if you are not modifying the parameter, then you should pass by constant reference. (For primitives or small non-polymorphic objects, you can also pass by value if you aren't modifying it).

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
2

Probably go with references, because they're cleaner. With pointers, you have a very awkward syntax.

As for validation, I would just do an ASSERT.

nbanic
  • 1,270
  • 1
  • 8
  • 11
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361