• Advertisement

Archived

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

C++ static_cast

This topic is 5898 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

This should be an easy question to answer I'm trying to cast using the C++ static_cast keyword instead of the C way of doing it. Here is the C method which works fine:
  
FileInput.read(( char * ) &BMPInput, sizeof( BMPInput ) );
  
But I get this error when trying to use the C++ keyword: error C2440: 'static_cast' : cannot convert from 'class CBmp *' to 'char *' Note: CBmp is my own class Here is my line of code that creates the error:
  
FileInput.read(static_cast<char *>(&BMPInput), sizeof( BMPInput ) );
  
Edited by - JohnG on February 26, 2002 2:05:54 AM

Share this post


Link to post
Share on other sites
Advertisement
static_cast is not meant for that, it is meant for conversion between types for which a conversion exists.

what you want is reinterpret_cast

it will work fine, as it is meant for casting between pointers of different types, or between pointers and integers.

Edited by - Smoogle on February 26, 2002 2:18:58 AM

Share this post


Link to post
Share on other sites
On the same topic...

I haven''t been able to find a way to use a C++ style-cast to do this conversion for me=].

From void* to HWND*, it only seems to work with a c-style cast. I used all four c++-style casts, and they all complain about the wrong types =/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I could be wrong but isn''t HWND already a pointer? If it is, say, a void *, you''re then trying to convert a void* to a void**.

Share this post


Link to post
Share on other sites
  FileInput.read(static_cast<BMPInput *>(&BMPInput), sizeof( BMPInput ) );  



Edited by - frikkasoft on February 26, 2002 5:03:26 AM

Share this post


Link to post
Share on other sites
To Anonymous Poster: Yes, that''s right... But I pass a void* the ADDRESS OF A HWND (I know it''s bad design, but it''s just for testing how c++ casts work), and you can''t dereference a void pointer, so I need to convert from void* to HWND*

Share this post


Link to post
Share on other sites

  • Advertisement