Thinking about C++ references

Started by
13 comments, last by MaulingMonkey 19 years, 5 months ago
Quote:Original post by Conner McCloud
Quote:Original post by Anonymous Poster
Quote:Original post by Conner McCloud
For all practical purposes, that's the method Java uses*. Personally, I don't like it, for readability purposes. I dislike functions that modify its arguments, so I prefer to assume they won't unless explicitly told otherwise. Defaulting to pass-by-value fits this scheme better.

CM

*Yes, yes, its passing a reference by value. Blah blah blah, either way you're passing a reference. No nit-picking.

Actually Java passes a pointer by value, not a reference. That's why you can call a function with null as parameter.

You're nit-picking. A pointer is a reference that doesn't neccessarily reference anything. Now, if we could please stop playing word games?

CM
i'm not quite sure if thats nit-picking or more of a simple correction ...

:)
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Advertisement
Quote:Original post by Conner McCloud
For all practical purposes, that's the method Java uses*. Personally, I don't like it, for readability purposes. I dislike functions that modify its arguments, so I prefer to assume they won't unless explicitly told otherwise. Defaulting to pass-by-value fits this scheme better.

CM

*Yes, yes, its passing a reference by value. Blah blah blah, either way you're passing a reference. No nit-picking.


I know you know how it works, but IMHO it makes a big difference.

Consider:

// Java, passing a primitive value:void foo(int x) {  x = 3; // not seen by caller!}// Java, passing objects:class Girl {  boolean considerSexWith(Boy b) {    // TODO: Comprehend the female mind and insert logic here.    // For now, assume success.    virginity = null;    return true;  }}class Boy {  void date(Girl g) {    if (g.considerSexWith(this)) brag(friends);    else g = thatGirlFromThePlayboyCalendar;    // ultimately though, this is only a fantasy.  }}


You can call methods on the input object, which may mutate the state of that object; but you cannot *replace it* (which is what is allowed to happen with pass-by-reference, more or less; depending on the definition of operator=, of course). Imagine how the Girl's father would react!
Quote:Original post by Timberl
if you pass by reference, you will generally never lose anything in terms of speed over passing by value, in fact you will probably gain speed because you don't have an internal copy command.

That depends. If you pass by reference, you still have to push the reference onto the stack. There is also the minor performance drain of dereferencing the reference. Usually, references will fit in a register, if the object being referenced fits in a register -- such as a bool, char, short int, int, float or double often will -- the speed advantage will usually be lost.
CoV
this section of the C++FAQ lite seems particularly relevant http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html

yes it seems references are pointers, so back to the drawing board for me
Quote:Original post by Mayrel
Usually, references will fit in a register, if the object being referenced fits in a register -- such as a bool, char, short int, int, float or double often will -- the speed advantage will usually be lost.


This deserves repeating. Small objects are passable by value just as easily as they are by reference.

Also, filling a structure with arguments then passing that by reference shouldn't be any faster than simply calling the function with said arguments. Well, unless the function needs to be called multiple times with the same (including recursively)/similar (including recursively if passing by non-const reference that we can freely modify) - but at this point we're micro-optimizing anyways, likely without having profiled first - which is a very bad thing (tm).

I pass classes by const reference usually (especially std::string, which although "small"ish, has assosicated memory (the string data) which needs to be copied if passing by value). If the function needs a temporary copy to modify without modifying the original, I pass by value.

This topic is closed to new replies.

Advertisement