Bap Johnston
Bap Johnston

Reputation: 359

Why does changing what a reference points to not throw an error?

Iv got to the stage in my c++ study concerning references. It states the following rule:

Once a reference is initialized to an object, it cannot be changed to refer to another object.

Iv wrote a short code (as asked to in an exercise) that is meant to prove this rule correct.

int y = 7;
int z = 8;

int&r = y;
r = z;

Can someone explain why this code compiles without any errors or warnings?

Upvotes: 4

Views: 360

Answers (6)

irwin
irwin

Reputation: 23

I faced the same issue when study <<thinking in c++> charpter11. here is my understanding code: the second compile error seems can not be simulated. but can understand. you can draw a picture to see what x, y, z, and ref2 point to.

/**
Write a program in which you try to
    (1) Create a reference that is not initialized when it is created.
    (2) Change a reference to refer to another object after it is initialized.
    (3) Create a NULL reference.
**/
#include <iostream>

using namespace std;

int main() {
    // 1 
    //int& ref1;      // compile error: 

    int x = 10;
    int& ref2 = x;
    cout << "x = " << x << endl;
    cout << "ref2 = " << ref2 << endl << endl;

    // 2 
    int y = 20;
    ref2 = y;   // 这里没有编译错误,ref2的指向并未改变,其指向的空间由10改变为20
    cout << "ref2 = " << ref2 << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl << endl;

    int z = 30;
    ref2 = z;
    cout << "ref2 = " << ref2 << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;

    // 3 
    //int& ref3 = NULL;   // compile error: 
    return 0;
}

Upvotes: 0

Naveen
Naveen

Reputation: 73473

When you do r = z you are not reseating the reference, instead you are copying the value of z into y.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308392

You're not changing the reference; you're setting a new value to the referred object. After this example you should note that y==8.

Upvotes: 3

James McNellis
James McNellis

Reputation: 355177

r = z does not change what r "points to." It assigns the value of z to the object pointed to by r.

The following code does the same thing as your code, but using pointers instead of references:

int y = 7;
int z = 8;

int* p = &y; // p points to y
*p = z;      // assign value of z to the object pointed to by p (which is y)

Upvotes: 8

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361612

int&r = y;
r = z;

It does NOT change the reference. Rather it changes the value pointed to by the reference variable. The reference variable is just yet another name of y. So r=z is equivalent to

y = z;

That is, r=z changes the value of y.

Reference variable cannot be reset to refer to another variable, in any way.

Upvotes: 4

Alok Save
Alok Save

Reputation: 206566

It does not make the reference alias to something else but it changes the value of what r contains.

int&r = y;

ris reference to y

r = z;

changes value of y & r to value of z by assigning value of z to r & hence y.

Upvotes: 4

Related Questions