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,259
  • 4
  • 27
  • 41

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
  • 90,931
  • 15
  • 133
  • 196
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,248
  • 1
  • 8
  • 11
Nathan Osman
  • 67,908
  • 69
  • 250
  • 347