473,888 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1789
In article <pa************ *************** *@www2.mrbrklyn .com>,
Ruben <ru***@www2.mrb rklyn.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.mrb rklyn.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.mrb rklyn.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
2466
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
1965
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 I have written a method that has exactly this which is the parameter is passed by constant references and I return constant references what is it that can cause common error by writng in this way.
8
3160
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, 1> and A<int, 2> are different types. Now, if the A template
2
6092
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 atleast two places where such a thing 'might' be used, though I am not clear whether it makes sense to have such a usage - 1) Function call int f (int& a); // a is an array of integer references
0
246
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 different projects.
2
1342
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 different projects.
7
1375
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 arr ;
11
1408
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
360
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" <mkwah...@mkwahler.netwrote: " An object will occupy memory space (in your program's memory area). A reference is simply another
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11176
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10772
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10880
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5812
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6012
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.