473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Address of a reference?

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
7 9522
"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

"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

"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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
4
by: Al Sashun | last post by:
Hi There, I'm not sure if this is the right place, but I'm after a script or program that will provide me with site visitors email address's, the only reason being my clients would like to see who...
2
by: tlotr | last post by:
Can I get my local IP address from javascript under IE? I've some pieces of code that work under netscape, but it doesnt under ie. Thanks in advance, tlotr
3
by: scott_baird | last post by:
I have an email macro setup (maybe I should go another way, but that was the quickest at the moment...) and what I would like to do is automate the "to" addressee of the email it generates for...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
6
by: Scott | last post by:
Hi All, I can't seem to wrap my head around this one. I have a pointer, int *x; which I can assign:
2
by: blufox | last post by:
What is difference between pass by address and pass by reference? Any pointers or links will be appreciated.
17
by: Henning Hasemann | last post by:
I have a function which gets the adress of an object as argument. It does some comparsion with the object's contents and then returns. no Reference or pointer to the object is stored or will be...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.