Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reference is not an object.

87 views
Skip to first unread message

wy

unread,
Nov 10, 2013, 8:08:32 PM11/10/13
to
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?

Leigh Johnston

unread,
Nov 10, 2013, 8:14:49 PM11/10/13
to
On 11/11/2013 01:08, wy wrote:
> 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;

Don't do "using namespace std;" as it is amateurish.

>
> 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 are misunderstanding the code above. Nowhere in that code is a
pointer to a reference being made; 'p' is the address of 'val' not the
address of the reference to 'val'.

/Leigh

Ian Collins

unread,
Nov 10, 2013, 8:18:28 PM11/10/13
to
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.

--
Ian Collins

wy

unread,
Nov 11, 2013, 8:23:40 PM11/11/13
to
On 11/11/2013 09:18 AM, Ian Collins wrote:
> 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

unread,
Nov 11, 2013, 9:10:55 PM11/11/13
to
No. "int &&" is reference to int rvalue. "int &*" is syntax error
that looks like someone wanted to declare pointer to reference.



sg

unread,
Nov 12, 2013, 7:07:00 AM11/12/13
to
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.

0 new messages