By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,137 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,137 IT Pros & Developers. It's quick & easy.

Address of a reference?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Howard" <al*****@hotmail.com> wrote in message
news:4L*********************@bgtnsc04-news.ops.worldnet.att.net...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2

P: n/a

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:te********************@twister.nyroc.rr.com.. .


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

--
Cy

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

Jul 22 '05 #3

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:%i*********************@bgtnsc04-news.ops.worldnet.att.net...

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:te********************@twister.nyroc.rr.com.. .


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

--
Cy

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
Jul 22 '05 #4

P: n/a
Howard wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

P: n/a
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
Jul 22 '05 #6

P: n/a
Howard wrote:
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."

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:4L*********************@bgtnsc04-news.ops.worldnet.att.net...
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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.