Archived

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

reinterpret_cast problem

This topic is 5235 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
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
quote:
Original post by derek6433

variable_type bob = reinterpret_cast |_variable_type| FindVariableByName("bob");

[edited by - derek6433 on August 5, 2003 12:54:47 AM]


Why do I see "reinterpret_cast" used so much? What is the point? I never used it and I didn't run into problems.

Hey, don't forget to visit my page... by clicking here!

[edited by - FrancoisSoft on August 13, 2003 2:35:28 PM]

Share this post


Link to post
Share on other sites
FrancoisSoft wrote ...
"Why do I see "reinterpret_cast" used so much? What is the point? I never used it and I didn''t run into problems."

If you have never used it, you are either using C-style casts, and that is _BAD_, or you have never come across a situation where you needed to use it.

In the OPs code he is converting void* to other variable types. As long as he is converting void pointers to some other pointers, a static_cast would be fine, but if he wanted to convert a void* to an int, for instance, static_cast wouldn''t work and he would have to use a reinterpret_cast.

Share this post


Link to post
Share on other sites
Why do you think do static_cast, reinterpret_cast, dynamic_cast and const_cast look as ugly as they do? Because they want to remind you that you are doing something ugly. Besides some basic convertions (float to int e.g.), casts are almost always a bad idea and show a lack of good design. Casting each and everything from and to void * or being dependent on such EReturnDataType-nonsens slaps OOP in its face!!

What do you think, templates, virtual functions, design patterns and so on are good for? :D

Regards, VizOne

Share this post


Link to post
Share on other sites
Is there a difference between reinterpret_cast and C-style casting? Besides the fact that one was hit with the ugly stick too many times..

Share this post


Link to post
Share on other sites
quote:
Original post by psykr
Is there a difference between reinterpret_cast and C-style casting? Besides the fact that one was hit with the ugly stick too many times..


That's kind of the point. The new casts stick out like a sore thumb, so they're like a big red flag over potentially dodgy code. That's particularly useful with reinterpret_cast because it's non-portable. Also, they're much easier to search for than a typename in parentheses, which makes it easier to find reinterpret_casts when you're porting code.

The other good thing about them is that they are more specific, so it's harder to get the wrong cast by mistake.

[edited by - Krunk on August 13, 2003 7:10:40 PM]

Share this post


Link to post
Share on other sites