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

Store an address of object

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
5 7075


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

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


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

Similar topics

5
by: Lars Behrens | last post by:
Hi there! For a web project I need a little expert help. I don't have written much code yet, just been fiddling around a bit, testing and planning. The web site will have a submission page for...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
5
by: Jay Villa | last post by:
I want to store multiple mailaddress like home address and shipping address Sometimes addresses will be more than two. I am thinking about storing the details as a dictionary object. Any other...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
3
by: Mochuelo | last post by:
Hi, In the constructor of a class, I am taking as a parameter the reference to a "bool", as in this code: ----------------------- public class TCP_synch_client { public string address...
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
3
by: Alfred | last post by:
I want to post text field data from these HTML TEXTAREA tags to a PostgreSQL database and have it reappear back on another page exactly as I had typed it. Over the years I have done this but only...
7
by: Nash | last post by:
Hi, I have a Client and Server application which does communication using sockets(APM). The problem is Whenever the client is connected to a server i am storing the socket in a list. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.