UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-06-01 09:17 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: static_cast

Hi
I was asked a question: If we relpace static_cast in a properly working code with reinterpret_cast, would it go something wrong? I said no, but i was wrong.Please,tell me somebody why?

Offline

#2 2007-06-02 12:48 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: static_cast

class A {...}
class B {...}
class C : public A, public B {...}

C * ptr = new C;
B * castOne = static_cast<B *>(ptr);  // correct!
B * castTwo = reinterpret_cast<B *>(ptr);  // wrong!! Points to A, not to B!

Offline

#3 2007-06-02 09:25 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: static_cast

Offline

#4 2007-06-04 07:33 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: static_cast

Offline

#5 2007-06-11 08:15 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: static_cast

Ok... have to admit I did NOT read the links in the third message of this thread but...

A static cast in C++ is really little more than the equivalent of a ( type ) var style cast in languages such as C...  and checks and validations along with warnings and errors are generated during the compilation cycle...

A reinterpret_cast is usually used to cast to ( and from ) ptrs to appropriate primitive integral type values... and on the 'stupid pet tricks' side to do things such as casting method / function ptrs from one type to another ( e.g. a function in 1 class to pretend to be a function in another class )...

So... since it seems the msg drifted back to vtables...

Perhaps what you are trying to do can be done but... Im not 100% certain what you are attempting to do...  and either way - I am going to guess that it is VERY dangerous...  and the reason why is a reinterpret_cast mostly just guarantees that if you do the cast in one direction, that a cast back in the other direction ( back to the original type ) will yield the same result... little to nothing more...

There are also issues with reinterpret_cast converting up and down the inheritance hierarchy for an object...  its not designed to do that - and though your compiler might allow it... uh... don't do it... the thing is that ( most of the ) compilers will not do full validation... they make certain only that things such as bit width ( e.g. ptr to int ) are compatible during compile... not during runtime...!

If you need to bounce up and down inheritance levels - use the dynamic_cast operation instead... it will delve into the RTTI ( run time type information ) level when necessary in a UNIFORM manner across compilers... it will catch outright errors during compilation... and the rest will be caught during run time ( std::bad_cast err )...

I am still curious about this code you are / have been working on... is it a legacy library that is not working correctly... or ?  A lot of items you have mentioned ( in other msgs ) seem to be aimed at working around deficiencies in the current object hierarchy / organization...  if you have the source... perhaps abstracting out another layer or two to use more static_casts and dynamic_casts ( where required and if possible ) - would be the best answer...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2009-06-04 08:01 AM

317681027
Guest

Re: static_cast

Board footer

Powered by FluxBB