1

I understood from C++ Primer that when I bind a const reference to a non const object, the reference is bound to a temporary object whos value is the non const object.

int a = 1; const int &b = a; a = 2; std::cout<<b; 

According to what I understood, a temporary const int object whos value is a will be created and b will be initialized with it, so, it is as if I wrote this code:

int a = 1; const int x = a; const int &b = x; a = 2; std::cout<<b; 

The first code prints 2, while the second prints 1. Why? Why did the value of the const reference b change with changing a while it is actually bound to a temporary const object not to a directly?

3
  • 1
    According to what I understood, a temporary const int object whos value is a will be created and b will be initialized with it, so, it is as if I wrote this code Is incorrect. No temporary value is involved. b refers directly to a. b CAN refer to a temporary (and will extend the lifespan of this temporary) but that is not what is happening here. Commented Mar 19, 2020 at 17:21
  • Does replacing std::cout<<b; with std::cout<<b<<"\n"; std::cout<<((a!=b)?a:b); show what's happening? Commented Mar 19, 2020 at 19:24
  • Take a look here: ideone.com/G8zmC2 Commented Mar 19, 2020 at 20:09

4 Answers 4

4

You are confusing two different things the C++ primer is saying.

First, you are using the term "const reference" to mean a reference that is itself constant. This is not what the C++ primer means by the term "const reference". As it says:

TERMINOLOGY: CONST REFERENCE IS A REFERENCE TO CONST
C++ programmers tend to cavalier in their use of the term const reference. Striclty speaking, what is meant by "const reference" is "reference to const". ... This usage is so common that we will follow it in this book as well.

Second, you are confusing examples and rules involving different base types (double and const int) with examples involving the same base type (int and const int).

Try this to get the effect you are describing:

double a = 42.0; const int &b = a; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering. I did use the term "const reference" to mean a reference to a const. The book mentioned the different base types example after it mentioned the examples of assigning const references to literals and same base type objects, so I thought they all have the same case.
2

I understood, a temporary const int object whos value is a will be created and b will be initialized with it,

You are wrong. Neither temporary object is created in this code snippet

int a = 1; const int &b = a; 

Moreover it is even unspecified in the C++ Standard whether a memory allocated for the reference b.

You should consider the reference b as an alias for the variable a.

As the reference refers the object a as a constant object you may not use the reference to change the object a. Nevertheless the object a is declared as a non-constant object. So you may change its value like

a = 2; 

but you may not change it using the reference like

b = 2; 

You could use the reference to change the value of the object a if the reference was declared like

int &b = a; 

In this case the result of these two statements

a = 2; 

and

b = 2; 

will be equivalent.

As for this code snippet

int a = 1; const int x = a; const int &b = x; a = 2; std::cout<<b; 

then the constant x is assigned with a copy of the value of the variable a. x and a are two different objects that occupy different extents of memory.

The reference b is declared as a reference to the object x.

const int &b = x; 

So changing the object a does not influence on the value of the constant x. The constant x may not be changed neither directly nor by using the reference b.

Comments

2
int a = 1; const int x = a; // x is a copy of a const int &b = x; // b is a reference to x, not a a = 2; std::cout<<b; 

In the second snippet, b is a reference to x. (Note that x is NOT a reference to a). So changing a doesn't change x, and so it doesn't change b.

Comments

0

Hello @Mason,

int a = 1; const int &b = a; a = 2; std::cout<<b; 

Here, the variable b is bound to a constant address or a "reference" which is, a. You can whatever you want to do to a but if you were to do:

int c = 7; &b = c; 

i.e., change the reference of b, you will get an error. b will hold whatever the value of whatever a as. This is kind of how pass/call by reference works.

int a = 1; const int x = a; const int &b = x; a = 2; std::cout<<b; 

Here, just like @cigien said,

In the second snippet, b is a reference to x. (Note that x is NOT a reference to a). So changing a doesn't change x, and so it doesn't change b.

your variable b has no relation to a. It is only related/referenced to x just like in the first code it was to a. Here, any change to a does not affect x. Hence, this justifies the answer ;)

Best.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.