Reference is not an object.

W

wy

I'm reading "C++ Primer", and it emphasizes "reference is not an object".
In P51, it says "Because references are not objects, we may not define a
reference to a reference." And in P52, it says "Because references are
not objects, they don't have addresses. Hence, we may not define a
pointer to a reference."

But the following code works with g++.

#include <iostream>

using namespace std;

int main()
{
int val = 0xfe;
int &ref= val;
int &r = ref;
int *p = &ref;
cout << "val = " << val << endl;
cout << "ref = " << ref << endl;
cout << "r = " << r << endl;
cout << "*p = " << *p << endl;
return 0;
}

Is the feature not standard, or do I misunderstand?
 
I

Ian Collins

wy said:
I'm reading "C++ Primer", and it emphasizes "reference is not an object".
In P51, it says "Because references are not objects, we may not define a
reference to a reference." And in P52, it says "Because references are
not objects, they don't have addresses. Hence, we may not define a
pointer to a reference."

But the following code works with g++.

#include <iostream>

using namespace std;

int main()
{
int val = 0xfe;
int &ref= val;
int &r = ref;
int *p = &ref;
cout << "val = " << val << endl;
cout << "ref = " << ref << endl;
cout << "r = " << r << endl;
cout << "*p = " << *p << endl;
return 0;
}

Is the feature not standard, or do I misunderstand?

You misunderstand!

int &r = ref;

doesn't declare a reference to a reference, it declares another
reference that is assigned the same value as ref.

int *p = &ref;

declares an int pointer to the address of whatever ref is bound to. Yo
can see this by adding the line

cout << "p = " << p << " &val = " << &val << endl;

int& *p;

would attempt to declare a pointer to a reference.
 
W

wy

int &r = ref;

doesn't declare a reference to a reference, it declares another
reference that is assigned the same value as ref.
I say this on P69 of the book. "When we use a reference, we are really
using the object to which the reference refers." Thank you!
int& *p;

would attempt to declare a pointer to a reference.
So "int &&" attempts to declare a reference to a reference, "int &*"
attempts to declare a pointer to a reference, right?
 
Ö

Öö Tiib

I say this on P69 of the book. "When we use a reference, we are really
using the object to which the reference refers." Thank you!


So "int &&" attempts to declare a reference to a reference, "int &*"
attempts to declare a pointer to a reference, right?

No. "int &&" is reference to int rvalue. "int &*" is syntax error
that looks like someone wanted to declare pointer to reference.
 
S

sg

Am 12.11.2013 02:23, schrieb wy:
So "int &&" attempts to declare a reference to a reference,

No. Careful. && is a single token. What you should have written is:

int & &

with a space in between the ampersands. But since this doesn't work
anyways, you can just forget about it. :)

Starting with C++11 the double-ampersand (as single token) can also be
used to declare a reference. It's another kind of reference:

int & lvalue reference to int
int && rvalue reference to int (NOT a ref to a ref!)
"int &*" attempts to declare a pointer to a reference, right?

Yes. But this also does not work, because a reference it not an object.
You can only create pointers to objects or pointers to functions.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top