how to understand references to variables and references to constants are distinguished?

B

baumann.Pan

in the book the C++ program language , 5.5 :
references to variables and references to constants are distinguished
because the introduction of a temporary in the case of the variable is
highly error-pron;
an assignment to the variable would become an assignment to the - soon
to disappear - temporary. No such problem exists for references to
constants.



I don't understand it. just before the sentence, it says
const double & cdr = 1; //ok
the interprtation of it might be:
double temp = double(1);
const double & cdr = temp;

according to it, it seems to me it is reference to constant introuce
temporary not reference to variable.
 
V

Victor Bazarov

in the book the C++ program language , 5.5 :
references to variables and references to constants are distinguished
because the introduction of a temporary in the case of the variable is
highly error-pron;
an assignment to the variable would become an assignment to the - soon
to disappear - temporary. No such problem exists for references to
constants.



I don't understand it. just before the sentence, it says
const double & cdr = 1; //ok
the interprtation of it might be:
double temp = double(1);
const double & cdr = temp;

I think the point here is that the '1' is not of type "double". It is
actually of type "int", so to make sure the 'cdr' refers to a double,
a temporary of type "double" is made from the (int)1.
according to it, it seems to me it is reference to constant introuce
temporary not reference to variable.

Correct. However, you should have the same effect (a temporary would
be introduced) if you do

int one = 1;
const double & cdr = one;

Here, 'cdr' is not a reference to 'one'. It's a reference to some
temporary object and you can confirm that by taking an address of it
and comparing it to the address of 'one'.

Victor
 
B

baumann.Pan

Victor Bazarov said:
I think the point here is that the '1' is not of type "double". It is
actually of type "int", so to make sure the 'cdr' refers to a double,
a temporary of type "double" is made from the (int)1.


Correct. However, you should have the same effect (a temporary would
be introduced) if you do

int one = 1;
const double & cdr = one;

is there any book on this topic?
 
V

Victor Bazarov

[...]
is there any book on this topic?

I am sure there are plenty. Start with "The C++ Programming Language"
by Bjarne Stroustrup, or "Accelerated C++" by Koenig and Moo. Then
there are "Effective" series by Meyers, et cetera, et cetera. Visit
www.accu.org, the book review section. Then look around for either
beginners or advanced or... Whatever you desire. Pay attention to
ratings.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top