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


Alright you C++ gurus :P

Recommended Posts

This has been bugging me for a long time now. Is there a difference between these two ways of casting?
This C++ tutorial that comes w/ VC++ 6.0 says the first way is better, yet they dont say why. Theres also the dynamic_cast and reinterpret_cast, of course, also. I know what these "new?" casts do but i dont see any differeneces in my programs between the two. -Snyper Edited by - Snyper on 10/5/00 5:18:42 PM

Share this post

Link to post
Share on other sites
well, I know that:

Is the same thing, but to tell u tha truth, Ive never seen the first one u put, Any one know?, now Im curious,

Share this post

Link to post
Share on other sites
--From wrox press c++ tutorial by ivor horton:
With mixed expressions involving the basic types, your compiler automatically arranges casting where necessary, but you can also force a conversion from one type to another by using an explicit cast. To cast the value of an expression to a given type, you write the cast in the form:


The keyword static_cast reflects the fact that the cast is checked statically - that is, when your program is compiled. Later, when we get to deal with classes, we will meet dynamic casts, where the conversion is checked dynamically - that is, when the program is executing. The effect of the cast is to convert the value that results from evaluating expression to the type that you specify between the angled brackets. The expression can be anything from a single variable to a complex expression involving lots of nested parentheses.

A dynamic cast is a cast that is performed at runtime. To specify a dynamic cast, you use the dynamic_cast<>() operator. You can only apply the dynamic_cast<>() operator to pointers and references to polymorphic class types - that is class types that contain at least one virtual function. The reason is that only pointers to polymorphic class types contain the information that the dynamic_cast<>() operator needs to check the validity of the cast. The dynamic_cast<>() operator is specifically for converting between pointers or between references to class types in a hierarchy. Note that the types you are casting between must be pointers or references to classes within the same class hierarchy (though not necessarily in a single leg of a hierarchy). You can''t use dynamic_cast<>() for anything else. We will look at dynamically casting pointers first.

two others:
reinterpret_cast<>() - this handles casts between unrelated types, for example, an integer to a pointer

const_cast<>() - this removes the const qualifier

Share this post

Link to post
Share on other sites
My take on the 'C' style casting vs static_cast is as follows.

'C' style casting i.e. 'i = (int)j' will always compile without error the compiler doing the best it can to convert the data. This may result in platform depended ( non-portable ) results.

Static casting i.e. 'i = static_cast<int>j' will only compile if the cast is reasonable ( The compiler knows how to perform the required conversion )

I guess MS recommend static_casts over 'C' casts as most non-portable casts are probable bugs/ typing errors ( i.e. missing * )

NB: C++ provides reinterpret_cast<> as a mechanism for performing non-portable casts.



// casts.cpp : checks which casts are valid


class C1
{ int i; };

class C2
{ float f; };

class C3 : public C2
{ long l; };

int main(int argc, char* argv[])

int i1 = 1, i2 = 2;
float f1, f2;
char c1, c2;
char *pc1, *pc2;
C1 foobar1, foobar2;
C2 *pfoo1, *pfoo2;
C3 junk;

f1 = (float)i1;
c1 = (char)f1;
pc1 = (char*)c1;
pfoo1 = (C2*)&foobar2;

f2 = static_cast<float>(i2);
c2 = static_cast<char>(f2);

// Line produces an error

pc2 = static_cast<char*>(c2);
// Line produces an error

pfoo1 = static_cast<C2*>(&foobar2);

pfoo1 = static_cast<C2*>(&junk);

return 0;

Edited by - TomH on October 6, 2000 8:29:19 AM

Edited by - TomH on October 6, 2000 8:30:39 AM

Share this post

Link to post
Share on other sites
The new C++-style casts are potentially safer; three of them are restricted in use (static, dynamic, const) while the reinterpret_cast is no worse than a C-style cast.

Another benefit of the new style casts: they're damn easy to find in source code. Set search to "cast".

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Edited by - mossmoss on October 6, 2000 8:36:06 AM

Share this post

Link to post
Share on other sites