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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  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
    #3
  4. <> 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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JIm Smith
    Replies:
    1
    Views:
    453
    VisionSet
    Jul 25, 2003
  2. Roger Leigh
    Replies:
    8
    Views:
    459
    Karl Heinz Buchegger
    Nov 17, 2003
  3. Ken North
    Replies:
    0
    Views:
    498
    Ken North
    Apr 2, 2008
  4. small Pox
    Replies:
    0
    Views:
    519
    small Pox
    Dec 10, 2010
  5. Simon Harrison
    Replies:
    2
    Views:
    137
    Simon Harrison
    Jul 22, 2006
Loading...

Share This Page