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.