Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

derek6433

reinterpret_cast problem

This topic is 5579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!