Store an address of object

W

Wei-Chao Hsu

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
 
K

Karl Heinz Buchegger

Wei-Chao Hsu said:
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 :)
 
R

Rolf Magnus

Wei-Chao Hsu said:
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.
 
W

Wei-Chao Hsu

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.
 
K

Karl Heinz Buchegger

Wei-Chao Hsu said:
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)
 
W

Wei-Chao Hsu

I have a library including a lot of class definitions but it is
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.
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top