Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reference to a reference

Reply
Thread Tools

Reference to a reference

 
 
Christian Meier
Guest
Posts: n/a
 
      10-27-2005
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> > particularly
> > http://www.parashift.com/c++-faq-lit...s.html#faq-8.6

>
> so, what's your point? what can be done with reference and can not be
> done with pointer, again?
>


Nothing. AFAIK references are usually implemented as pointers.
But references exist because they are more convenient to use because
references can't be NULL.

Greetings Chris


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-27-2005

Senthil wrote:
> Thanks for all your replies..Just to make clear that i've understood it
> properly,
> The following should be invalid program right ?
>
> int main()
> {
> int i = 10;
> int &r1 = i;
> int &r2 = r1;
>
> return 0;
> }
>
> Best Regards,
> Senthil


No. That is valid. Both r1 and r2 refer to the same object (in this
case, i).

Cheers! --M

 
Reply With Quote
 
 
 
 
Christian Meier
Guest
Posts: n/a
 
      10-27-2005
"Senthil" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) ups.com...
> Thanks for all your replies..Just to make clear that i've understood it
> properly,
> The following should be invalid program right ?
>
> int main()
> {
> int i = 10;
> int &r1 = i;
> int &r2 = r1;
>
> return 0;
> }
>
> Best Regards,
> Senthil
>



No, this program is valid. r2 is a reference to an int as you can see in
your declaration of r2:
> int &r2 = r1;


But as already mentioned before, you can't do something like
int&& r2 = r1;

Greetings Chris


 
Reply With Quote
 
makc.the.great@gmail.com
Guest
Posts: n/a
 
      10-27-2005

mlimber wrote:
> references are necessary for operator overloading:
> We couldn't do this with pointers.


okay, agreed...

though this is because of the way operators are "implemented" or
"defined" in c++. I can imagine a language, where there would be no
difference between "variables" and "pointers" presented to programmer,
so that constants would be only objects....

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      10-27-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> > particularly
> > http://www.parashift.com/c++-faq-lit...s.html#faq-8.6

>
> so, what's your point? what can be done with reference and can not be
> done with pointer, again?


You've snipped a lot of context. You originally asked two questions.

1 what are references are good for?
2 what can be done with references, that cannot be done with pointers?

faq 8.6 answers question 1.

I also pointed you at all of section 8
http://www.parashift.com/c++-faq-lite/references.html
Read this whole section and you will find the answer to question 2 (e.g
faq 8.3).

HTH
Gavin Deane

 
Reply With Quote
 
makc.the.great@gmail.com
Guest
Posts: n/a
 
      10-27-2005

Christian Meier wrote:
> because
> references can't be NULL.


how about:
int* i = 0; int &j = *i;

it gives me no errors, no warnings.

 
Reply With Quote
 
makc.the.great@gmail.com
Guest
Posts: n/a
 
      10-27-2005
oh wait. then we would loose math operations with pointers.

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      10-27-2005

(E-Mail Removed) wrote:
> Christian Meier wrote:
> > because
> > references can't be NULL.

>
> how about:
> int* i = 0; int &j = *i;
>
> it gives me no errors, no warnings.


So you compiled it. Did you run it?

Gavin Deane

 
Reply With Quote
 
makc.the.great@gmail.com
Guest
Posts: n/a
 
      10-27-2005

(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > Christian Meier wrote:
> > > because
> > > references can't be NULL.

> >
> > how about:
> > int* i = 0; int &j = *i;
> >
> > it gives me no errors, no warnings.

>
> So you compiled it. Did you run it?
>
> Gavin Deane


yes, it runs fine.
there can't be error until you USE j. and I do not :-p

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      10-27-2005
(E-Mail Removed) wrote in news:1130423577.642411.300190
@g43g2000cwa.googlegroups.com:

>
> (E-Mail Removed) wrote:
>> Christian Meier wrote:
>> > because
>> > references can't be NULL.

>>
>> how about:
>> int* i = 0; int &j = *i;
>>
>> it gives me no errors, no warnings.

>
> So you compiled it. Did you run it?


It may run too (without crashing). Doesn't matter. Undefined Behaviour
has been invoked by dereferencing a NULL pointer. To the OP: remember that
when one says that references may not be "NULL", there is an implicit "in a
well-formed C++ program" attached to the end of the statement. By
dereferencing a NULL pointer, the program is no longer well-formed. Try to
make a reference to "NULL" without invoking undefined behaviour first.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET 2.0: master pages and web user controls: reference to a non-shared member requires an object reference bminder ASP .Net 0 06-24-2005 12:22 AM
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM
web reference interfering with reference to component Dude ASP .Net 0 11-09-2004 11:53 AM
How to tell if a reference is project or file reference from within the IDE? Darren ASP .Net 0 10-11-2004 12:51 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments