| View previous topic :: View next topic |
| Author |
Message |
Senthil Guest
|
Posted: Thu Oct 27, 2005 1:10 pm Post subject: Reference to a reference |
|
|
Hi,
I am reading Modern C++ Design where Anderi quoted "C++ does not allow
references to references".
Assume i have
DoThat(const string& strData2)
{
...
}
DoThis(const string& strData)
{
..
DoThat(strData);
}
int main()
{
string Data;
DoThis(Data);
}
Now the strData i have inside DoThis is a reference to Data.The
strData2 I have is a reference to strData.i.e strData2 is a reference
to a reference.
Here Andrei's statement confuses me..Am i missing something?
Thanks,
Senthil
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Thu Oct 27, 2005 1:16 pm Post subject: Re: Reference to a reference |
|
|
Senthil wrote:
| Quote: | Hi,
I am reading Modern C++ Design where Anderi quoted "C++ does not allow
references to references".
Assume i have
DoThat(const string& strData2)
{
...
}
DoThis(const string& strData)
{
..
DoThat(strData);
}
int main()
{
string Data;
DoThis(Data);
}
Now the strData i have inside DoThis is a reference to Data.The
strData2 I have is a reference to strData.i.e strData2 is a reference
to a reference.
Here Andrei's statement confuses me..Am i missing something?
Thanks,
Senthil
|
What's the context (section number and paragraph)? Perhaps he means
that you can't do with references what you can do with pointers:
int anInt = 42;
int *pointer_to_int = &anInt;
int **pointer_to_pointer_to_int = &pointer_to_int;
Cheers! --M
|
|
| Back to top |
|
 |
Dan Cernat Guest
|
Posted: Thu Oct 27, 2005 1:21 pm Post subject: Re: Reference to a reference |
|
|
Senthil wrote:
| Quote: | Hi,
I am reading Modern C++ Design where Anderi quoted "C++ does not allow
references to references".
Assume i have
DoThat(const string& strData2)
{
...
}
DoThis(const string& strData)
{
..
DoThat(strData);
}
int main()
{
string Data;
DoThis(Data);
}
Now the strData i have inside DoThis is a reference to Data.The
strData2 I have is a reference to strData.i.e strData2 is a reference
to a reference.
Here you are wrong. strData2 is a reference to a string according with |
its declaration.
| Quote: | Here Andrei's statement confuses me..Am i missing something?
Thanks,
Senthil
|
/dan
|
|
| Back to top |
|
 |
makc.the.great@gmail.com Guest
|
Posted: Thu Oct 27, 2005 1:48 pm Post subject: Re: Reference to a reference |
|
|
mlimber wrote:
| Quote: | you can't do with references what you can do with pointers:
int anInt = 42;
int *pointer_to_int = &anInt;
int **pointer_to_pointer_to_int = &pointer_to_int;
|
than what are references are good for? what can be done with
references, that cannot be done with pointers?
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Oct 27, 2005 1:52 pm Post subject: Re: Reference to a reference |
|
|
* Senthil:
| Quote: |
I am reading Modern C++ Design where Anderi quoted "C++ does not allow
references to references".
Assume i have
DoThat(const string& strData2)
{
...
}
DoThis(const string& strData)
{
..
DoThat(strData);
}
int main()
{
string Data;
DoThis(Data);
}
Now the strData i have inside DoThis is a reference to Data.The
strData2 I have is a reference to strData.i.e strData2 is a reference
to a reference
|
No, strdata2 refers to the same object that strData refers to.
| Quote: | Here Andrei's statement confuses me..Am i missing something?
|
Yes, in addition to the above, and what Andrei's sentence is all about:
that C++ doesn't currently allow the syntax T&&, or U& when U is a
reference type, which is the probem for template code (it can be solved,
as Andrei shows, but it's awkward).
Bjarne Stroustrup has suggested (possibly this was just with regard to
template code) that T&& should be allowed, and defined to mean T&.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Oct 27, 2005 1:52 pm Post subject: Re: Reference to a reference |
|
|
Senthil wrote:
| Quote: | Hi,
I am reading Modern C++ Design where Anderi quoted "C++ does not allow
references to references".
Assume i have
DoThat(const string& strData2)
|
According to the declaration, strData2 is a reference to const string.
| Quote: | {
...
}
DoThis(const string& strData)
|
According to the declaration, strData is a reference to const string.
| Quote: | {
..
DoThat(strData);
}
int main()
{
string Data;
DoThis(Data);
}
Now the strData i have inside DoThis is a reference to Data.The
strData2 I have is a reference to strData.
|
It's a refernce to Data.
| Quote: | i.e strData2 is a reference to a reference.
|
No. If allowed, a reference to a reference would be declared something like:
const string&& strData2
|
|
| Back to top |
|
 |
deane_gavin@hotmail.com Guest
|
|
| Back to top |
|
 |
makc.the.great@gmail.com Guest
|
Posted: Thu Oct 27, 2005 2:12 pm Post subject: Re: Reference to a reference |
|
|
so, what's your point? what can be done with reference and can not be
done with pointer, again?
|
|
| Back to top |
|
 |
Senthil Guest
|
Posted: Thu Oct 27, 2005 2:13 pm Post subject: Re: Reference to a reference |
|
|
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
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Thu Oct 27, 2005 2:15 pm Post subject: Re: Reference to a reference |
|
|
[email]deane_gavin (AT) hotmail (DOT) com[/email] wrote:
Also, references are necessary for operator overloading:
#include <ostream>
using namespace std;
class Foo { /* ... */ };
ostream& operator<< ( ostream& os, const Foo& foo )
{
// Stream elements of Foo here ...
return os;
}
void Bar( const Foo& foo )
{
cout << "Here's your Foo:n" << foo << endl;
// ...
}
We couldn't do this with pointers.
Cheers! --M
|
|
| Back to top |
|
 |
Christian Meier Guest
|
Posted: Thu Oct 27, 2005 2:17 pm Post subject: Re: Reference to a reference |
|
|
<makc.the.great (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1130422344.258473.298980 (AT) g47g2000cwa (DOT) googlegroups.com...
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
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Thu Oct 27, 2005 2:18 pm Post subject: Re: Reference to a reference |
|
|
Senthil wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Christian Meier Guest
|
Posted: Thu Oct 27, 2005 2:20 pm Post subject: Re: Reference to a reference |
|
|
"Senthil" <s.senthilvel (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1130422385.054927.50140 (AT) g14g2000cwa (DOT) googlegroups.com...
| Quote: | 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:
But as already mentioned before, you can't do something like
int&& r2 = r1;
Greetings Chris
|
|
| Back to top |
|
 |
makc.the.great@gmail.com Guest
|
Posted: Thu Oct 27, 2005 2:26 pm Post subject: Re: Reference to a reference |
|
|
mlimber wrote:
| Quote: | 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....
|
|
| Back to top |
|
 |
deane_gavin@hotmail.com Guest
|
Posted: Thu Oct 27, 2005 2:30 pm Post subject: Re: Reference to a reference |
|
|
[email]makc.the.great (AT) gmail (DOT) com[/email] wrote:
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
|
|
| Back to top |
|
 |
|