Need help with reinterpret_cast, a C-style cast or function-style cast 
Author Message
 Need help with reinterpret_cast, a C-style cast or function-style cast

Hi ,

I would like to cast the memory address of this structure (const DataRec
*psDataRec)  to a long and put this address  into the variable  lDataRec.

The following statement

long  lDataRec;
lDataRec   =  reinterpret_cast<long*>(const_cast<DataRec*>(&psDataRec[0]));

gives this error message
error C2440: '=' : cannot convert from 'long *' to 'long'    This conversion
requires a reinterpret_cast, a C-style cast or function-style cast

What should I do different, your help is greatly appreciated.

Regards
Guno



Tue, 19 Oct 2004 07:31:55 GMT  
 Need help with reinterpret_cast, a C-style cast or function-style cast
What about:
 lDataRec = (long) psDataRec;
or
 lDataRec = (long) &psDataRec[0];



Quote:
> Hi ,

> I would like to cast the memory address of this structure (const DataRec
> *psDataRec)  to a long and put this address  into the variable  lDataRec.

> The following statement

> long  lDataRec;
> lDataRec   =

reinterpret_cast<long*>(const_cast<DataRec*>(&psDataRec[0]));
Quote:

> gives this error message
> error C2440: '=' : cannot convert from 'long *' to 'long'    This
conversion
> requires a reinterpret_cast, a C-style cast or function-style cast

> What should I do different, your help is greatly appreciated.

> Regards
> Guno



Tue, 19 Oct 2004 08:00:58 GMT  
 Need help with reinterpret_cast, a C-style cast or function-style cast
The problem is that you are casting to a long* and then trying to assign
that to a long.

I would do it this way:

        long  lDataRec =
reinterpret_cast<long>(const_cast<DataRec*>(psDataRec));

I would do this *only* if you really need to put it in a long. If not, I'd
make it 64-bit ready:

        LONG_PTR lpDataRec =
reinterpret_cast<LONG_PTR>(const_cast<DataRec*>(psDataRec));

LONG_PTR is 32-bits on Win32 and 64-bits on Win64 so you won't have to
change the code if you port to Win64.

Tim



Tue, 19 Oct 2004 08:05:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help with reinterpret_cast, a C-style cast or function-style cast

2. Need help with reinterpret_cast, a C-style cast or function-style cast

3. Need help with reinterpret_cast, a C-style cast or function-style cast

4. old style casts vs new style casts

5. old style casts vs new style casts

6. C++ style cast

7. static_cast or C-styled cast?

8. Discarded Function Values (To Cast or Not to Cast)

9. casting of struct element void pointer casting

10. Cast...always cast...

11. malloc: to cast or not to cast

12. to cast or not to cast?

 

 
Powered by phpBB® Forum Software