Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 3 characters in body
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

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 is referencerefers the object aa 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 notnor by using the reference b.

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 is reference 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 not by using the reference b.

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.

Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

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 is reference 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 not by using the reference b.