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

yurec;21993 wrote:

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?

I think reinterpret_cast would do the wrong thing when dealing with multiple inheritance... i.e.

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

I read http://carcino.gen.nz/tech/cpp/multiple_inheritance_this.php and http://en.wikipedia.org/wiki/Virtual_function_table

As I understand the following should be correct:

class A { virtual void f(){}; };
class B { };
class C : A,B { };

vtable for A would be
+0 : A::f
+4 : A::~A

vtable for C::A would be

+0 : A::f
+4 : C::~C

vtable for C::B would be
+0 : C::~C

Is it correct?

Offline

#4 2007-06-04 07:33 PM

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

Re: static_cast

yurec;21996 wrote:


As I understand the following should be correct: [...]

It might be correct for a particular compiler, but you shouldn't assume any particular vtable layout if you want your code to remain portable.  The C++ standard doesn't specify how vtables are to be laid out in memory, so different compilers are free to implement it different ways.

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