Reference to a reference

S

Senthil

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
 
M

mlimber

Senthil said:
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
 
D

Dan Cernat

Senthil said:
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.
Here Andrei's statement confuses me..Am i missing something?

Thanks,
Senthil

/dan
 
M

makc.the.great

mlimber said:
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?
 
A

Alf P. Steinbach

* Senthil:
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.

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&.
 
R

Rolf Magnus

Senthil said:
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.
{
...
}

DoThis(const string& strData)

According to the declaration, strData is a reference to const string.
{
..
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.
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
 
S

Senthil

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
 
M

mlimber


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
 
C

Christian Meier

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
 
M

mlimber

Senthil said:
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
 
C

Christian Meier

Senthil said:
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
 
M

makc.the.great

mlimber said:
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....
 
D

deane_gavin

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
 
A

Andre Kostur

(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top