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

Discussion in 'C++' started by baumann.Pan@gmail.com, Nov 9, 2004.

1. ### Guest

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.

, Nov 9, 2004

2. ### Victor BazarovGuest

Re: how to understand references to variables and references to constantsare distinguished?

wrote:
> 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

Victor Bazarov, Nov 9, 2004

3. ### Guest

Victor Bazarov <> wrote in message news:<UY6kd.8392\$09.us.to.verio.net>...
> wrote:
> > 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;
>

is there any book on this topic?
> 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

, Nov 10, 2004
4. ### Victor BazarovGuest

<> wrote...
> [...]
> 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

Victor Bazarov, Nov 10, 2004