Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++.moderated > #7311

References to const

Message-ID <vb9ux.54392$UI6.3334@fx45.am4> (permalink)
Newsgroups comp.lang.c++.moderated
From Robert <rob1@invalid.net>
Subject References to const
Organization virginmedia.com
Date 2015-07-29 16:30 -0600

Show all headers | View raw


Hi, I am reading C++ Primer but am having some difficulty understanding 
references to const. I am hoping that this turns out to be quite a 
simple topic and I find that I am only thinking it is complex.

I understand that a reference is simply another name for an object and 
that we can bind references to objects that are non const or const. 
Further a reference to const is unique in that it cannot be used to 
change the object to which the reference is bound.

My difficulty in understanding concerns the initialization rules for 
references to const.

There is a general rule in c++ (subject to two exceptions) which says 
that the type of a reference must match exactly the type of the object 
to which it refers when it is initialized. Now according to my book, one 
exception to this rule concerns a reference to const:

C++ Primer page 61 "..we can initialize a reference to const from any 
expression that can be converted to the type of the reference. In 
particular, we can bind a reference to const to a nonconst object, a 
literal, or a more general expression."

What is the reason for this exception?

Well reading on, the book does not (in my opinion) explain this section 
very well. I cannot understand what follows next and have quoted it 
verbatim in case you don't have the book:

"The easiest way to understand the difference in initialization rules is 
to consider what happens when we bind a reference to an object of a 
different type:

double dval = 3.14;
const int &ref = dval;

Here ref refers to an int. Operations on ref will be integer operations, 
but dval is a floating point number, not an integer. To ensure that the 
object to which ref is bound is an int, the compiler transforms this 
code into something like

const int temp = deval; // create a temporary const int from the double
const int &ref = temp; bind ref to that temporary

In this case, ref is bound to a temporary object. A temporary object is 
an unnamed object created by the compiler when it needs a place to store 
a result from evaluating an expression...

Now consider what could happen if this initialization were allowed but 
ref was not a const. If ref weren't const, we could assign to ref. Doing 
so would change the object to which ref is bound. That object is a 
temporary, not dval. The programmer who made ref refer to dval would 
probably expect that assigning to ref would change dval. After all, why 
assign to ref unless the intent is to change the object to which ref is 
bound? Because binding a reference to a temporary is almost surely not 
what the programmer intended, the language makes it illegal."

----
I have read this several times today and seem to be getting more 
confused each time I read it so will perhaps come back to it in the 
morning. I would really appreciate it if someone who has a clear 
understanding of this issue could explain this to me in a simple step by 
step manner! Why can a reference to const be bound to an object that 
does not match the reference type exactly? Why would anyone want to do 
this in real code? Alternatively, could you recommend any other text 
books or articles that might help me?


I think part of my problem is that I cannot see how this might apply to 
real code - it all seems very abstract. However,I have a feeling that 
this is a really *really* important basic topic that I need to grasp 
before moving on.

Best wishes


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Back to comp.lang.c++.moderated | Previous | NextNext in thread | Find similar


Thread

References to const Robert <rob1@invalid.net> - 2015-07-29 16:30 -0600
  Re: References to const legalize+jeeves@mail.xmission.com (Richard) - 2015-07-29 20:41 -0600
    Re: References to const legalize+jeeves@mail.xmission.com (Richard) - 2015-07-31 07:12 -0600
      Re: References to const "Alf P. Steinbach" <alf.p.steinbach+usenet@googlemail.com> - 2015-08-01 07:32 -0600
  Re: References to const Martin Bonner <martinfrompi@yahoo.co.uk> - 2015-07-30 11:01 -0600
  Re: References to const Jack Adrian Zappa <adrianh.bsc@googlemail.com> - 2015-09-04 07:53 -0600
    Re: References to const Martin Bonner <martinfrompi@yahoo.co.uk> - 2015-09-11 07:01 -0600
      Re: References to const Öö Tiib <ootiib@hot.ee> - 2015-09-11 12:32 -0600
        Re: References to const James Kuyper <jameskuyper@verizon.net> - 2015-09-12 13:57 -0600

csiph-web