Simple C++ reference question

Status
Not open for further replies.
I've been doing it the following way in my programming (apologies for using sorghumking's template):

Code:
void main()
{
   int foo = 5;
   increment_foo(&foo);
}

void increment_foo(int *bar)
{
   *bar++;
}

So foo is a declared variable with a set amount of memory, then you pass a reference to that variable. The function takes a reference, then increments the variable at that memory address.

Works for me, anyhow... :)
 

w.anders

Ars Tribunus Angusticlavius
6,957
When declaring a function that changes its argument, it's more polite to use a pointer, thereby clearly indicating that the argument variable is going to be modified by the function. Compare the function calls:

Code:
fun(arg); // Reference argument, no indication that arg will be changed.
Code:
fun(&arg); // Pointer argument, clear indication that arg is likely to be changed.
 
When declaring a function that changes its argument, it's more polite to use a pointer, thereby clearly indicating that the argument variable is going to be modified by the function. Compare the function calls:
[...]
Hmm, I'm not sure I agree with this. One can distingusih between the two situations with const references, and the compiler will go some way to tray and enforce that, too. Given declarations
Code:
int function_one(Foo &f);
int function_two(const Foo &f);
it can be seen that function_one might want to modify its argument, but that function_two will not.
The advantage of using const references instead of pass-by-value is that it can sometimes avoid unnecessary copies.
 

w.anders

Ars Tribunus Angusticlavius
6,957
Yes, when looking at the definition, it's plainly visible whether the function can change its argument. However, when seeing the function call, giving the address to the argument with the & operator shows clearly that the function isn't making a copy.

I'll throw in some appeals to authority, too. ;)

To keep a program readable, it is often best to avoid functions that modify their arguments. Instead, you can return a value from the function explicitly or require a pointer argument [excluded a code example] The increment(x) notation doesn't give a clue to the reader that x's value is being modified, the way x=next(x) and incr(&x) does. Consequently "plain" reference arguments should be used only where the name of the function gives a strong hint that the reference argument is modified.

-- Bjarne Stroustrup, The C++ Programming Language, Third edition

Of course, regarding const references:

As mentioned in §5.5, functions that modify call-by-reference arguments can make programs hard to read and should most often be avoided [..] It can, however, be noticeable more efficient to pass a large object by reference than to pass it by value. In that case, the argument might be declared const to indicate that the reference is used for efficiency reasons only and not to enable the called function to change the value of the object [..]

-- Bjarne Stroustrup, The C++ Programming Language, Third edition

(All quoted text above copyright (c) 1997 by AT&T, blah blah fair use blah blah... :D )
 
Yes, when looking at the definition, it's plainly visible whether the function can change its argument. However, when seeing the function call, giving the address to the argument with the & operator shows clearly that the function isn't making a copy.
Yup, it's a fair cop :)

I'm trying now to think of an example where one might prefer to use a non-const reference instead of a pointer. The only one that comes to mind is functional-style classes (with an operator()), but given the restrictions on taking references of references, non-const versions of these often don't play well with the STL anyway.

I don't have a copy of Stroustroup's book - does he mention any?
 
Status
Not open for further replies.