Robert
2015-07-29 22:30:22 UTC
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
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! ]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]