reinterpret_cast problem

Started by
14 comments, last by derek6433 20 years, 8 months ago
Ok, I have a function that returns a (void *) from a linked list. The purpose of this void * is obviously to allow any variable, no matter what type, to be pointed to. When I use this function I am using it with reinterpret_cast like so And because of some umm technical difficulties with the html I was forced into use "|"'s of "< & >"'s variable_type bob = reinterpret_cast |_variable_type| FindVariableByName("bob"); I want to though,use reinterpret_cast iwthout know what the variables type is. ............................. Don't know that is an int ......................................\/ int bob = reinterpret_cast |int| FindVariableByName("bob"); Is there any way I can find what type 'bob'(the variable) is so I can use that type in the reinterpret_cast? Heres what I mean in psuedo-code Is there like a function or something that returns a type? ........................................\/ int bob = reinterpret_cast |typeofvariable(bob)| FindVariableByName("bob"); I know I could check the size of the pointer with all of the variable types to determine what type it is, But that would be very ineffecient Any help would be appreciated, Derek [edited by - derek6433 on August 5, 2003 12:54:47 AM]
Advertisement
actually, checking the size of the pointer will always give you 4 bytes ( on x86 32 bit machine ) since the pointer size is constant no matter what it point to.

Shortly, there's no function that will tell you what a void pointer is pointing too. But, what you could do is return a struct with 2 things inside: an enum stating what is the type of the pointed object and a void * to hold the actual data.

enum EReturnDataType{    eRDT_INT,    eRDT_FLOAT,    eRDT_CHAR,    eRDT_STRING,    eRDT_MAX};struct ReturnData{    EReturnDataType   m_eDataType;    void*             m_pData;}// and to use it ...if( MyReturnData.m_eDataType == eRDT_INT ){    int iBob = (int) MyReturnData.m_Data;// or if m_Data point to an int pointer//  int *piBob = reinterpret_cast<int*>( MyReturnData.m_Data );}

take this as pseudo-code and not a tested solution but it should help you with your problem.

[edited by - gizz on August 5, 2003 12:37:21 AM]
I ment to say checking the size of what the pointer was pointing to.

Yah I guess I''ll have to do what you said, although it would be pretty cool if I could do it the way I wanted to origionally.
quote:Original post by derek6433
I ment to say checking the size of what the pointer was pointing to.

Yah I guess I''ll have to do what you said, although it would be pretty cool if I could do it the way I wanted to origionally.


Sorry, can''t be done. Values don''t have type information in C/C++.

The exception is classes and RTTI, which can be helpful through dynamic_cast (not reinterpret_cast). Not useful in your case though.
Use static_cast to cast to/from void* and concrete types that are stored in there.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:Original post by derek6433
I ment to say checking the size of what the pointer was pointing to.


How would you have checked the size of what it is pointing to since the pointer contain only an address that can be anything ?
One of Microsoft''s many API''s has a class named VARIANT, which might solve your problem. It contains a union of many many types, an enum describing which one of the types it is, and methods to convert between them.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Ugh...type safety is out the door with this design...you should definitely rethink it.
quote:Original post by Neosmyle
Ugh...type safety is out the door with this design...you should definitely rethink it.



No... It works fine as it is. I haven't had any problems with reinterpret_cast at all in this program.

[edited by - derek6433 on August 8, 2003 12:24:20 PM]
What exactly are you doing with this anyway?
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement