C++ references

Member
Posts: 321
Joined: 2004.10
Post: #1
In C++, are reference variables pretty much only used in function/method parameters?

I can't seem to find any examples in Google where this is _not_ the case.



I'm declaring a C++ class with alot of data members (mainly pointers) whose values once set, will never change during the lifetime of my program.

So trying to be a good programmer, I think to my self: wouldn't a reference be good here? But then I think that maybe a const pointer const (or whatever syntax you do to make the pointer constant and the memory that the constant pointer points to - be constant) would be better here?

Anyone have a rule of thumb here or just an opinion. Cause I don't Smile
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
You can pretty much assume that references are only useful for passing values into functions and doing funky things with return values. There are (as always) exceptions, but go with the above, and if you find another good use for it, go with it.

Read http://www.parashift.com/c++-faq-lite/re...ml#faq-8.6 to see what Grand Master Cline has to say on it. Smile
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
Since references are pointers with less power, you can imagine that their uses are limited. They are useful for function parameters, though, just so it reduces the syntax. Everywhere else, and arguably even for that, it's kind of pointless to use them.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
I've found in the past that GCC did a better job of optimization when parameters to inline functions were passed as const T& than when they're passed as const T*. Go figure Smile

To the OP: If you don't understand references, you should never use them for anything other than function parameters. Even if you do understand references, there aren't *that* many more uses for them Smile
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #5
Thanks for all the help. Just wanted to share.

I didn't write this, but I wish I had. Gave me a perspective that I'd never
came across before:


... statistically, most C/C++ function calls don't use pass by value so forcing programmers to override the default passing mechanism using the &, * and -> operators is an example of a bad language design choice. C++ creators were aware of this. They introduced a new type of argument passing, namely pass by reference. The addition of reference variables and arguments to C++ was only a means of fixing an historical accident made in C about a decade earlier rather than a genuine innovation


In most cases, references are used as a means of passing arguments to a function by reference. The nice thing about references is that they function as pointers from a compiler's point of view, although syntactically they behave like ordinary variables. They enable a callee to alter its arguments without forcing programmers to use the unwieldy *, & and -> notation

Passing by reference combines the benefits of passing by address and passing by value. It's efficient, just like passing by address because the callee doesn't get a copy of the original value but rather an alias thereof (under the hood, all compilers substitute reference arguments with ordinary pointers). In addition, it offers a more intuitive syntax and requires less keystrokes from the programmer. Finally, references are usually safer than ordinary pointers because they are always bound to a valid object -- C++ doesn't have null references so you don't need to check whether a reference argument is null before examining its value or assigning to it.
Passing objects by reference is usually more efficient than passing them by value because no large chunks of memory are being copied and constructor and destructor calls are performed in this case. However, this argument passing mechanism enables a function to modify its argument even if it's not supposed to. To avert this, declare all read-only parameters as const and pass them by reference. This way, the callee will not be able to modify them
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #6
Various scripting languages always pass objects as reference, no matter if you need to change them or less. Its not half bad IMO.

I read a few articles deprecating references, mainly because they are an unnecessary complication. Though IMO they actually make the syntax a bit easier.

In C++ I always pass stuff by variable if I dont have to change it, though passing as a const reference is actually an idea...

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
Remember, you should (almost) always pass objects that your ABI will place in registers by value, and large objects by pointer or reference... for in-between objects, there's a tradeoff, and it's not always obvious which way to go.

The compiler will optimize register-passable objects in const references as parameters to inline functions, so you should almost always use const T& in templates.
Quote this message in a reply
Post Reply