473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

references to constant objects

re-reviewing the chapter in Lippan and Lajoie C++ Primer the have a
section on references that I don't understand and just makes e shake my
head believing that any behavior is possible with references and that they
can never be understood. Maybe someone else can rephrase this so that I
understand the defined behavior.

cont int ival = 1024;

//error: requires a const reference
int *&pr_ref = &ival; //<<== this seems obvious as a const obj must use
a pointer to a constant object

"Our first attempt to correct the definition is pi_ref might be the
following, but it dos not work-do you see why?

//Still and error (and no, I don't see why)

const int *&pi_ref = &ival;

"If we read this definition from right to left, we discover that pi_ref is
a reference to a pointer to an object of type in defined to be a const (to
which I'm think - yeah - so what! That is exactly what I need!) Our
reference isn't to a constant but rather a noncontant pointer that
addresses a contant object( and why does that not work! i need a point to
a const, not a const pointer!). the correct definition is as follows:

const int ival = 1024;

//ok: this is accepted

int * const &pi_ref = &ival;

(Why - through this I can change the value of a constant object to any
integer)
Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 13 '08 #1
6 1764
In article <pa****************************@www2.mrbrklyn.com> ,
Ruben <ru***@www2.mrbrklyn.comwrote:
[...]
cont int ival = 1024;

//error: requires a const reference
int *&pr_ref = &ival; //<<== this seems obvious as a const obj must use
a pointer to a constant object

"Our first attempt to correct the definition is pi_ref might be the
following, but it dos not work-do you see why?

//Still and error (and no, I don't see why)

const int *&pi_ref = &ival;
For the same reason that this is an error:

int i = 10;
double& d = i; // error

If it were allowed, modifications to d would modify a temporary, which
is probably not what the programmer intended. If he really wanted a
reference and wasn't going to modify its referent, he should make it to
a const object:

int i = 10;
double const& d = i; // OK
Sep 13 '08 #2
On Sat, 13 Sep 2008 10:35:34 -0500, blargg wrote:
>const int *&pi_ref = &ival;

For the same reason that this is an error:

int i = 10;
double& d = i; // error

If it were allowed, modifications to d would modify a temporary, which is
probably not what the programmer intended. If he really wanted a reference
and wasn't going to modify its referent, he should make it to a const
object:

int i = 10;
double const& d = i; // OK

Thank for the swift answer. This makes e ask two questions though. Why
is a temp value being made in this case and how do we reconcile that the
type being referenced is still a point to a const int?

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 13 '08 #3
Ruben wrote:
On Sat, 13 Sep 2008 10:35:34 -0500, blargg wrote:
>>const int *&pi_ref = &ival;

For the same reason that this is an error:

int i = 10;
double& d = i; // error

If it were allowed, modifications to d would modify a temporary, which is
probably not what the programmer intended. If he really wanted a
reference and wasn't going to modify its referent, he should make it to a
const object:

int i = 10;
double const& d = i; // OK


Thank for the swift answer. This makes e ask two questions though. Why
is a temp value being made in this case and how do we reconcile that the
type being referenced is still a point to a const int?
Well, pi_ref is a reference to a pointer. But where is that pointer? Can you
name it?

Sep 13 '08 #4
In article <ga*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.dewrote:
Ruben wrote:
On Sat, 13 Sep 2008 10:35:34 -0500, blargg wrote:
>const int *&pi_ref = &ival;

For the same reason that this is an error:

int i = 10;
double& d = i; // error

If it were allowed, modifications to d would modify a temporary, which is
probably not what the programmer intended. If he really wanted a
reference and wasn't going to modify its referent, he should make it to a
const object:

int i = 10;
double const& d = i; // OK
Thank for the swift answer. This makes e ask two questions though. Why
is a temp value being made in this case and how do we reconcile that the
type being referenced is still a point to a const int?

Well, pi_ref is a reference to a pointer. But where is that pointer? Can you
name it?
Maybe a closer analog to his pointer example is this:

int i = 0;
double& d = (double) i; // error

Sure, we're explicitly making the double (pointer in the original
example), but it's still a temporary.
Sep 13 '08 #5
On Sep 13, 8:03 am, Ruben <ru...@www2.mrbrklyn.comwrote:
const int ival = 1024;

//ok: this is accepted

int * const &pi_ref = &ival;

(Why - through this I can change the value of a constant object to any
integer)
It shouldn't be accepted. Both consts are needed, like this:

const int * const & pi_ref = &ival;

&ival is a const int*, so the first const is needed. The second const
is needed because you're taking a reference to a temporary. Leaving
out either gives an error (g++ 4.1.3)

--Mark McKenna
Sep 13 '08 #6
On Sat, 13 Sep 2008 13:25:29 -0700, mqrk wrote:
On Sep 13, 8:03 am, Ruben <ru...@www2.mrbrklyn.comwrote:
>const int ival = 1024;

//ok: this is accepted

int * const &pi_ref = &ival;

(Why - through this I can change the value of a constant object to any
integer)

It shouldn't be accepted. Both consts are needed, like this:

const int * const & pi_ref = &ival;

&ival is a const int*, so the first const is needed. The second const is
needed because you're taking a reference to a temporary. Leaving out
either gives an error (g++ 4.1.3)

--Mark McKenna
That's what I thought. I should give this a try and see how it works.

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rickard Andersson | last post by:
What it the use of constant references in methods and functions? Is it a design issue or hwat? A reference that cannot be edited? huh? Why pass a reference then?
6
by: Tony Johansson | last post by:
Hello Experts! I'm reading in a book about C++ and the book says common errors is "A function should not return a constant references to its parameter passed by constant references" Why? Here...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: Neelesh | last post by:
Hi all, References are (almost always) implemented as constant pointers. We can certainly have an array of constant pointers. But then why we cannot have array of references? I can think of...
0
by: sonali_reddy123 | last post by:
Hi all, I have developed an application in which there are uptill now 12 projects and few of them are dependent on each other. The problem is I am not able to manage the references across...
2
by: sonu | last post by:
Hi all, I have developed an application in which there are uptill now 12 projects and few of them are dependent on each other. The problem is I am not able to manage the references across...
7
by: zombek | last post by:
Hi. I'd like to know which is faster and when to use: this (int type is just an example I mean it generally): int* ptr = new int (123); int* arr = new int ; and this: int num = 123; int...
11
by: desktop | last post by:
I have made this example: #include<iostream> class Beer { public: Beer() { std::cout << "made a beer\n"; num = 1; }
2
by: subramanian100in | last post by:
From http://groups.google.com/group/comp.lang.c++/browse_frm/thread/d5da6e5e37fd194d/6e2e8424a1cfbd2b#6e2e8424a1cfbd2b the following portion is taken. "Mike Wahler"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.