473,498 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regarding FAQ 8.5

Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" , the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
cout << ref << endl;
return 0;
}

I used g++ to compile the above program.

Oct 11 '05 #1
5 1049

<ja**********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" , the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?
Because it's valid.

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
This is equivalent to

i = j;

The reference still refers to 'i' (iow 'ref' is still
an alias for 'i').

cout << ref << endl;
This should print 0. Does it?
return 0;
}

I used g++ to compile the above program.


-Mike
Oct 11 '05 #2
On 11 Oct 2005 11:47:25 -0700, ja**********@yahoo.com wrote:
Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" ...
You cannot.
... the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
// OK: ref is an alias for i.
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
You assign the value of j to i here. You haven't "changed the
reference variable", nor can you.
cout << ref << endl;
return 0;
}

I used g++ to compile the above program.


--
Bob Hairgrove
No**********@Home.com
Oct 11 '05 #3
<ja**********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" , the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
cout << ref << endl;
return 0;
}

I used g++ to compile the above program.


No, you're not.
In this case ref = j; is equivalent to i = j;
cout << i << endl; after you do ref=j; you'll see for yourself.

Martin
Oct 11 '05 #4
In article <11**********************@g43g2000cwa.googlegroups .com>,
<ja**********@yahoo.com> wrote:
Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" , the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
cout << ref << endl;
return 0;
}

I used g++ to compile the above program.


That does not "reseat" ref. One way to look at this is to
consider that ref becomes another name for i. Therefore,
since you can say i = j, which doesn't mean i become j
but that j's value is copies into i, the same applies here.
Since ref is another name for i, then ref = j is as if it
were i = j.

If you prefer to stay with the constand pointer analogy you
might have:

int * const ref = &i;

i = j; // as usual
*ref = j; //since *ref is also another 'name' for i, then as if: i = j

ref = &j // error
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 11 '05 #5
In article <di**********@panix2.panix.com>,
Greg Comeau <co****@comeaucomputing.com> wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
<ja**********@yahoo.com> wrote:
Hi,
According to the FAQ [8.5] "How can you reseat a reference to make it
refer to a different object?" , the reference is like a constant
pointer.Then why the following program is not giving any compilation
error/s ?

#include <iostream>
using namespace std;

int main(void)
{
int i=9,j=0;
int &ref=i;
cout<< ref << endl;
ref= j; // I'm changing the reference variable.
cout << ref << endl;
return 0;
}

I used g++ to compile the above program.


That does not "reseat" ref. One way to look at this is to
consider that ref becomes another name for i. Therefore,
since you can say i = j, which doesn't mean i become j
but that j's value is copies into i, the same applies here.
Since ref is another name for i, then ref = j is as if it
were i = j.

If you prefer to stay with the constand pointer analogy you
might have:

int * const ref = &i;

i = j; // as usual
*ref = j; //since *ref is also another 'name' for i, then as if: i = j

ref = &j // error


Also, remember that most operations on references are
actually operations on the thing it references. IOWs,
in this case, after ref is "bound", operations on ref
are actually operations on i.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 11 '05 #6

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

Similar topics

4
1787
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP....
3
1761
by: praba kar | last post by:
Dear All, I am new to Python. I am in need of some sorting functions (eg) numerical sorting functions and alphapetical sorting functions. I have searched through net But I cannot find any...
3
2183
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
7
2436
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
8
5206
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
2
1610
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
5
1787
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
1
1501
by: Dave | last post by:
Hi guys, i have a small question regarding register_globals I have managed to change the way my forms work so they will process with register_globals turned off. However i have one page which...
1
1733
by: Jim Flanagan | last post by:
Hello - I am in need of more help regarding an approach to accomplishing the following: We have a need to change the Volume serial numbers of a bunch of preprogrammed IDE Solid State Drive...
8
2016
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
7125
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
7004
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...
0
7167
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,...
1
6890
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...
0
7379
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...
1
4915
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...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
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...

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.