0

Possible Duplicates:
When pass-by-pointer is preferred to pass-by-reference in C++?
Are there benefits of passing by pointer over passing by reference in C++?
How to pass objects to functions in C++?

Hi all, I'm working to develop a large object oriented c++ application framework as part of my chemical engineering graduate research.

I find that many of my functions take pointers to custom objects or STL objects. I find that in terms of writing code, this makes it harder to access functions or variables stored within.

Aside from simplicity, though, is there any advantages/disadvantages to passing by reference v. passing by pointer?

If there is an advantage to one over the other, I'll probably look to refactor my code to uniformly use whatever approach is optimal. If there isn't I may still refactor to consistently use pass by reference for readability (i.e. not having to dereference)

Again I want to make the best decision as my framework is already 40+ files large, so I want to implement as uniform structure as I can, as early as I can, and with whatever the optimal method is.

Thanks in advance!

Community
  • 1
  • 1
Jason R. Mick
  • 5,177
  • 4
  • 40
  • 69
  • 3
    Harder? Typing `->` instead of `.`? – Beta Sep 01 '10 at 20:14
  • 1
    This is another way to ask "I know it has been asked multiple times already. But let me ask it again, maybe you won't notice that it's a dupe." really :) See the "Related" link. Lots of the same questions. – Johannes Schaub - litb Sep 01 '10 at 20:17
  • 1
    BTW according to the c++ gist, the more angle brackets there are, the better the code is. So I recommend neither to harden on by-value nor on by-reference. But on `boost::call_traits::param_type`. Have fun – Johannes Schaub - litb Sep 01 '10 at 20:21
  • @Beta ... and "(&..." on the var pass ... ;o) ...as to this being a dupe my apologies. Should I close this topic via delete? I looked at the first few links but they didn't sound like they were asking my question. – Jason R. Mick Sep 01 '10 at 20:30

6 Answers6

6

The main difference is that a reference cannot be NULL (at least not without some malicious programming). For syntactical sugar and when an argument is required, I'd pass by reference. If I had a situation where the argument were optional, I'd pass by pointer.

There are always exceptions. If it conforms to the current style, or due to things I have to do with it, it may be more convenient to have it as a pointer.

krousey
  • 1,728
  • 15
  • 22
  • 1
    +1. Personally, I use references when the object is not optional, and pointers otherwise. That's the convention that is being used at my company as well. – Shautieh Sep 01 '10 at 20:32
  • 1
    +1 for `at least not without some malicious programming` – phimuemue Sep 01 '10 at 20:42
2

Prefer pass by reference. If for nothing else I do it because a reference simply can't be NULL. It puts an end to so many stupid bugs, debates, contract checks, memory responsibility questions, etc ...

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Of course a reference can be NULL. Take for example `int& f(int* p) { return *p; }` and the call `int* x = NULL; int& y = f(x);`. I can't count the times I've seen this bug in many forms. But semantically speaking, I agree. If you want to "talk about something" instead of "saying where the thing you're talking about is located", use a reference. – Laurent LA RIZZA Jun 24 '14 at 11:51
2

FYI this was asked in a slightly diff way earlier today:

When to pass by reference and when to pass by pointer in C++?

Canonical advice is "pass by ref to const unless a v good reason for another choice".

Community
  • 1
  • 1
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

If your choice is pointers vs. references, use references. They have all the advantages of pointers without the danger of dereferencing an uninitialized pointer.

And don't worry too much about implementing a uniform approach early-- in this case it really isn't difficult to go back and forth between the two (one function at a time) even when the code base is large.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • You can still dereference an uninitialized pointer if the reference has been constructed by dereferencing an uninitialized pointer. – Laurent LA RIZZA Jun 24 '14 at 11:49
0

Personally I like passing by pointer because it offers some level of consistency in my code. I automatically know that my object Foo is a pointer. The same would apply with a reference so be sure to pick one and stick with it.

mj_
  • 6,297
  • 7
  • 40
  • 80
0

While I like references, there are a few common cases where you can't use them:

  • You want to store an object reference/pointer in a member variable but don't want to require that object in your class constructor
  • You want to store an object reference/pointer in a member variable and change the instance at runtime
  • You want make an object parameter to a method or function optional

In my view, having a mix of references and pointers is pretty ugly, so I prefer pointers.

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58