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

Store an address of object

P: n/a
Hi,

I try to put an address of object into an int. Like this

myclass obj;
int address;
address=&obj;

"myclass" is an object of class or struct. It seems to illegal to C++. Is
there any way to do this task?

Dennis
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Wei-Chao Hsu wrote:

Hi,

I try to put an address of object into an int. Like this

myclass obj;
int address;
address=&obj;

"myclass" is an object of class or struct. It seems to illegal to C++. Is
there any way to do this task?


First of all your example raises the question: Why?
Why would you want to store the address of an object in an int.
You have no guarantee that an int will be large enough to hold
an address.

That said: Yes, it is illegal to do this. But with a cast you can
always force your compiler to do things which are illegal :-)

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

P: n/a
Wei-Chao Hsu wrote:
Hi,

I try to put an address of object into an int. Like this

myclass obj;
int address;
address=&obj;
Don't. It's a bad idea.
"myclass" is an object of class or struct. It seems to illegal to C++.
C++ has strong type checking, and only pointers are supposed to contain
addresses.
Is there any way to do this task?


If you think you really must do this, you can use a reinterpret_cast:

address = reinterpret_cast<int>(&obj);

Note that this is unportable and will e.g. break on most 64bit
platforms.

Jul 22 '05 #3

P: n/a

Wei-Chao Hsu wrote:
Hi,

I try to put an address of object into an int. Like this

myclass obj;
int address;
address=&obj;

"myclass" is an object of class or struct. It seems to illegal to C++. Is
there any way to do this task?

First of all your example raises the question: Why?
Why would you want to store the address of an object in an int.
You have no guarantee that an int will be large enough to hold
an address.

That said: Yes, it is illegal to do this. But with a cast you can
always force your compiler to do things which are illegal :-)

I have a library including a lot of class definitions but it is
impossible to be used by another language, like FORTRAN. So I want to
create a new object in C++ function and then pass the address of the
object to a FORTRAN function. If the object is needed to do something,
pass the address from FORTRAN to C++ function and assign the address to
an object.

This is my idea and I am not a C++ expert. I do not know whether this
mechanism could be performed.

Jul 22 '05 #4

P: n/a


Wei-Chao Hsu wrote:

Wei-Chao Hsu wrote:
Hi,

I try to put an address of object into an int. Like this

myclass obj;
int address;
address=&obj;

"myclass" is an object of class or struct. It seems to illegal to C++. Is
there any way to do this task?

First of all your example raises the question: Why?
Why would you want to store the address of an object in an int.
You have no guarantee that an int will be large enough to hold
an address.

That said: Yes, it is illegal to do this. But with a cast you can
always force your compiler to do things which are illegal :-)

I have a library including a lot of class definitions but it is
impossible to be used by another language, like FORTRAN. So I want to
create a new object in C++ function and then pass the address of the
object to a FORTRAN function. If the object is needed to do something,
pass the address from FORTRAN to C++ function and assign the address to
an object.


OK. As said, you need to cast things.

int address = (int)&obj;

and when you get the int back:

myclass* pTheObj;
pTheObj = (myclass*) TheInt;

pTheObj->DoSomething();

But convince yourself at least 3 times, that you won't loose any
information when storing the address in a int (and an INTEGER*4)

This is my idea and I am not a C++ expert. I do not know whether this
mechanism could be performed.


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

P: n/a
> > I have a library including a lot of class definitions but it is
impossible to be used by another language, like FORTRAN. So I want to
create a new object in C++ function and then pass the address of the
object to a FORTRAN function. If the object is needed to do something,
pass the address from FORTRAN to C++ function and assign the address to
an object.


OK. As said, you need to cast things.

int address = (int)&obj;

and when you get the int back:

myclass* pTheObj;
pTheObj = (myclass*) TheInt;

pTheObj->DoSomething();

But convince yourself at least 3 times, that you won't loose any
information when storing the address in a int (and an INTEGER*4)


Thank you! It works.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.