Address of a reference?

H

Howard

Hi,

in one of the recent posts, I saw someone pass two variables of built-in
type (int and double), by reference, to a member function of a class. That
function then took the addresses of those reference parameters and stored
the results in member pointer variables.

Is that guaranteed to be ok? In other words, does taking the address of
a reference to a built-in type give you the address of the original
variable? I'd expect so (not having a clue what it might do instead) but it
seems very strange to me.

Personally, I'd have used pointers as the parameters themselves (passing
the address of the variables to the function), and then just copied the
pointers' contents to my member variables. But I'm just wondering if it's
ok (and/or normal?) to take the address of a reference to a variable (of
built-in type, which has no member operator & to call, as a user-defined
object might).

Thanks,
-Howard
 
C

Cy Edmunds

Howard said:
Hi,

in one of the recent posts, I saw someone pass two variables of
built-in type (int and double), by reference, to a member function of a
class. That function then took the addresses of those reference
parameters and stored the results in member pointer variables.

Is that guaranteed to be ok? In other words, does taking the address
of a reference to a built-in type give you the address of the original
variable? I'd expect so (not having a clue what it might do instead) but
it seems very strange to me.

Personally, I'd have used pointers as the parameters themselves
(passing the address of the variables to the function), and then just
copied the pointers' contents to my member variables. But I'm just
wondering if it's ok (and/or normal?) to take the address of a reference
to a variable (of built-in type, which has no member operator & to call,
as a user-defined object might).

Thanks,
-Howard

Taking the address of a reference is perfectly legal, but I agree with you
that pointers might have been more intuitive.
 
H

Howard

Cy Edmunds said:
Taking the address of a reference is perfectly legal, but I agree with you
that pointers might have been more intuitive.


Thanks. That's what I guessed.

That makes for one other difference between pointers and references - one I
don't recall seeing in answers to questions that ask what the difference is:
Taking the address of a pointer gives you, well, the address of the
pointer, NOT the address of the object being pointed to. But taking the
address of a reference DOES give you the address of the object referred to,
not the address of the reference variable (which, I am guessing, may or may
not exist as a separate, space-consuming entity in a given implementation).

-Howard
 
J

John Harrison

Howard said:
Thanks. That's what I guessed.

That makes for one other difference between pointers and references - one
I don't recall seeing in answers to questions that ask what the difference
is: Taking the address of a pointer gives you, well, the address of the
pointer, NOT the address of the object being pointed to. But taking the
address of a reference DOES give you the address of the object referred
to, not the address of the reference variable (which, I am guessing, may
or may not exist as a separate, space-consuming entity in a given
implementation).

-Howard

Any operation on a reference operates on the object referred to, I can't
think of any exceptions. There is no way to refer to a reference, as it
were.

john
 
K

Karl Heinz Buchegger

Howard said:
Thanks. That's what I guessed.

That makes for one other difference between pointers and references - one I
don't recall seeing in answers to questions that ask what the difference is:
Taking the address of a pointer gives you, well, the address of the
pointer, NOT the address of the object being pointed to. But taking the
address of a reference DOES give you the address of the object referred to,
not the address of the reference variable (which, I am guessing, may or may
not exist as a separate, space-consuming entity in a given implementation).

That's because officially there is no such thing as a 'reference variable'.
A reference is another name to an existing object. That's all. So for all
purposes the reference and the other object it stands, for are indistinguishable.
Everything you do with the reference is the same as if you did it with the
object the reference stands for.
 
J

JKop

Howard posted:
Hi,

in one of the recent posts, I saw someone pass two variables of
built-in
type (int and double), by reference, to a member function of a class.
That function then took the addresses of those reference parameters and
stored the results in member pointer variables.

Is that guaranteed to be ok? In other words, does taking the
address of
a reference to a built-in type give you the address of the original
variable? I'd expect so (not having a clue what it might do instead)
but it seems very strange to me.

Personally, I'd have used pointers as the parameters themselves
(passing
the address of the variables to the function), and then just copied the
pointers' contents to my member variables. But I'm just wondering if
it's ok (and/or normal?) to take the address of a reference to a
variable (of built-in type, which has no member operator & to call, as
a user-defined object might).

Thanks,
-Howard


#include <iostream>

int main()
{
double k = 56;

double &j = k;

double &h = j;

double &f = h;

if ( (&k == &j) && (&h == &k) ) ;
else
{
std::cout << "Broken compiler.";
}


//Just as the following is UB:

++k = --k;


//So is the following:

++k = --f;
}


A reference in all cases is just "another name" for a different object.


The only place where they become magical is with function arguments and with
return values from functions, ie. it's as if the *called* function has
access to the *calling* function's local objects. But the same can be
achieved with pointers.


-JKop
 
I

Ioannis Vranos

Howard said:
Hi,

in one of the recent posts, I saw someone pass two variables of built-in
type (int and double), by reference, to a member function of a class. That
function then took the addresses of those reference parameters and stored
the results in member pointer variables.

Is that guaranteed to be ok? In other words, does taking the address of
a reference to a built-in type give you the address of the original
variable? I'd expect so (not having a clue what it might do instead) but it
seems very strange to me.

Personally, I'd have used pointers as the parameters themselves (passing
the address of the variables to the function), and then just copied the
pointers' contents to my member variables. But I'm just wondering if it's
ok (and/or normal?) to take the address of a reference to a variable (of
built-in type, which has no member operator & to call, as a user-defined
object might).


Pointers are distinct objects themselves and they occupy space.
References on the other hand are not considered as distinct objects, but
as aliases (other names) of objects.


As it is written in TC++PL:

"In some cases, the compiler can optimize away a reference so that there
is no object representing that reference at runtime."
 
M

Mike Wahler

Howard said:
Hi,

in one of the recent posts, I saw someone pass two variables of built-in
type (int and double), by reference, to a member function of a class. That
function then took the addresses of those reference parameters and stored
the results in member pointer variables.

Is that guaranteed to be ok? In other words, does taking the address of
a reference to a built-in type give you the address of the original
variable? I'd expect so (not having a clue what it might do instead) but it
seems very strange to me.

Personally, I'd have used pointers as the parameters themselves (passing
the address of the variables to the function), and then just copied the
pointers' contents to my member variables. But I'm just wondering if it's
ok (and/or normal?) to take the address of a reference to a variable (of
built-in type, which has no member operator & to call, as a user-defined
object might).

A reference doesn't have an address. A reference is not an object.
Applying the address operator to a reference yields the address of
the object to which it refers.

int i;
int& r = i;
int *p = &r; /* gets address of the object 'i', not the reference 'r' */

-Mike
 

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
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top