473,385 Members | 1,531 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,385 software developers and data experts.

Address of a reference

Is this ok?

#include <iostream>

int main(int argc, char *argv[])
{
int x=1234;
int *x_ptr=&x;
int &x_ref=x;

std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
int *y=&x_ref;
std::cout << std::hex << y << "=" << *y << std::endl;

return 0;
}
Cant find anything in my standard about taking addresses of
references.

Adrian

Mar 26 '07 #1
5 2069
I should have added, that I actually want a pointer to the original
object but only have a reference.

Mar 26 '07 #2
Adrian wrote:
Is this ok?

#include <iostream>

int main(int argc, char *argv[])
{
int x=1234;
int *x_ptr=&x;
int &x_ref=x;

std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
int *y=&x_ref;
std::cout << std::hex << y << "=" << *y << std::endl;

return 0;
}
Cant find anything in my standard about taking addresses of
references.
You can't take the address of a reference. A reference is like an alias
for the object it is referencing.

You can "contain" a reference by placing it in a class; e.g.

template <typename Tstruct Ref
{
T & v;
Ref( T & iv ) : v( iv ) {}
};

int main()
{
int x=1234;
Ref<int refx( x );
}

however, it's somewhat useless because you can't reseat a reference
after it has been initialized (unlike a pointer).

The truly nasty hack you can to to reseat a reference is to reconstruct
the class in place (as I said - a major bad).
int main()
{
int x=1234;
Ref<int refx( x );

int y=444;

refx.~Ref<int>();

new (&refx) Ref<int>( y );
}

(I'm not sure it's compilable but you get the idea)

It is perfectly legitimate C++ code but I think you will be ridiculed if
you use it like I will be for pointing it out :-)....
Mar 26 '07 #3
Adrian wrote:
Is this ok?

#include <iostream>

int main(int argc, char *argv[])
{
int x=1234;
int *x_ptr=&x;
int &x_ref=x;

std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
int *y=&x_ref;
std::cout << std::hex << y << "=" << *y << std::endl;

return 0;
}
Cant find anything in my standard about taking addresses of
references.
That's because references don't have addresses.
In your code, y will just contain the adress of the object that the
reference refers to.

Mar 26 '07 #4
Adrian wrote:
I should have added, that I actually want a pointer to the original
object but only have a reference.
Taking the address of a reference will give you the address of the
object the reference is referring to.
Mar 26 '07 #5
On Mar 26, 4:28 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
Adrian wrote:
I should have added, that I actually want a pointer to the original
object but only have a reference.

Taking the address of a reference will give you the address of the
object the reference is referring to.
Thanks, that what I wanted and thats what my example does - but wanted
to make sure that is what it is supposed to do :-)
Mar 27 '07 #6

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
7
by: Howard | last post by:
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.