3

In the "C++ programming language", there is an example

void g()
{  
   int ii = 0;
   int& rr = ii;
   rr++;
   int* pp = &rr;
}

The author states:

This is legal, but rr++ does not increment the reference rr, rather, ++ is applied to an int that happens to be ii.

I am quite confusing about this statement, why "rr++ does not increment the reference rr"? So rr is just used as "bridge" to increment ii?

3
  • A reference is not a pointer. You can't modify a reference, you can modify the value stored in the referenced memory. Commented Oct 6, 2011 at 20:06
  • 1
    I find the author's comment extremely confusing. Just realize that rr is not an object in its own right, but merely an alternate name for some other, pre-existing object. So operations cannot be performed on rr itself, but rather the other object to which it refers. In this case, rr++; is identical in effect to what ii++; would have been. Commented Oct 6, 2011 at 20:08
  • 2
    Saying that rr is an alternate name for another preexisting object is IMO more confusing because it just doesn't fit how things work. A reference is not another name, but it is its own entity. For example int &r = *new int; delete &r; after this, r still exists, the object it referred to doesn't exist anymore. The reference has a name, it is not a name itself. Commented Oct 6, 2011 at 20:27

4 Answers 4

3

Yes, in C++ a reference is nothing more than an alias, or a "bridge" as you say. Any time you say rr, it is as if you'd said ii, the "original" name of the variable that rr references.

That is different from references in many other languages (which behave more like C++'s pointers). In C++, references are not objects, they have no separate identity. You can't tell the reference to "please increment", or "please point to null" or anything else. It just becomes another name for the variable that the reference points to.

Sign up to request clarification or add additional context in comments.

Comments

1

A reference is similar to a dereferenced pointer to a variable. For example:

int v = 3;
int* pv = &v;
int& rv = v;

// Access v through a pointer:
*pv = 4;

// Access v through a reference:
rv = 4;

In this example, both statements will set v to 4.

Comments

0

So rr is just used as "bridge" to increment ii?

Exactly. Internally, it would store an address of ii and the compiler would increment the int at this address. Can be used for eg.:

int left, right;
...
int &direction = random_bool() ? left : right;
++direction; // would change left or right, depending on random_bool()

but is mostly used in function parameters and return types.

2 Comments

I doubt very much that there will be an address here internally. Rather, the compiler will emit code that will increment ii’s value directly.
@KonradRudolph: gcc with no optimization certainly stores the address on the stack and uses it. Saying that the compiler may optimize that out is pointless - it could, but it would probably treat pointers equally, it could optimize out the incrementing (assigning ii initially 1 instead of 0) and it could even eliminate the ii variable altogether. A good optimizing compiler will surely do all of these, turning g() into a no-op. However, this says nothing about the behavior of references.
0

It increments ii, it does not increment an address that is contained in rr.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.