|
Both static_cast and reinterpret_cast seem to work fine for casting void* to another type. Is there a good reason to favor one over the other?
Thanks, Andy
Started by Andy on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
Rather, reinterpret_cast....
You should use a reinterpret_cast , because that describes better what you're doing (completely ignoring type safety)
You should use a reinterpret_cast , because that describes better what you're_cast .
|
|
I am reasonably proficient in C++, but I do not have a lot of experience using the cast operators to convert pointers of one type to another. I am familiar with the risks and benefits of pointer casting, as well as the evils of using C-style casts. What...
Started by e.James on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
The difference with static_cast is that a dynamic_cast does runtime checking which may (safer) or may not (more overhead....
Does this answer your question?
I have never used reinterpret_cast , and wonder whether running are used a lot.
|
|
I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts (i.e.
MyClass *m = (MyClass *)ptr;
all over the place, but there seem to be two other...
Started by Graeme Perrow on
, 11 posts
by 10 people.
Answer Snippets (Read the full thread at stackoverflow):
It contains a good ....
For complete information, see the following MSDN link/Type_Casting .
Dynamic_cast has runtime type checking and only works with references and pointers, whereas static_cast does not offer runtime type checking.
|
Ask your Facebook Friends
|
What is implicit_cast? when should I prefer implicit_cast rather than static_cast?
Started by yesraaj on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Int i = 100; long l = i;
and
int i = 100; long l for your own types, by overloading implicit... .
Implicit_cast transforms one type to another, and can be extended by writing implicit cast functions, to cast from one type to another.
|
|
Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is their any sort of speed difference?
Started by dicroce on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
The generality of the C-style cast can be overkill.
Are you up/down casting? Are you reinterpreting to find a specific type of cast in a large codebase.
A plain C-style cast can mean a lot of things.
|
|
Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
I don't quite get when to use static cast and when dynamic. Any explanation please?
Started by rayimag on
, 8 posts
by 8 people.
Answer Snippets (Read the full thread at stackoverflow):
For example....
Static_cast is just a compile time cast, checks if origin class can be promoted to the casted class by some simple rules as inheritance.
Dynamic cast requires RTTI and does some magic compared to static cast.
|
|
Often, especially in Win32 programming it is required to cast from one opaque type to another. For example:
HFONT font = cast_here<HFONT>( ::GetStockObject( SYSTEM_FONT ) );
Both static_cast and reinterpret_cast are applicable here and have exactly...
Started by sharptooth on
, 9 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
Static....
More information at this website about which casts should be used when.
Reinterpret_cast is the least typesafe cast.
Static_cast is always preferable, avoid doing reinterpret_cast s unless absolutely neccessary.
|
|
Try to see which cast is faster (not necessary better): new c++ case or old fashion C style cast. Any ideas?
Started by Andrei on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
When it comes to such basic constructs as a single cast, once two shouldn't use C-cast, instead use C++ casts - it will allow you to find errors at compile time..
No difference whatsoever.
Optimizations.
|
|
Hi,
I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast :
T1 * p1=... void *pv=p1; T2 * p2= static_cast<T2*>(pv);
instead of:
T1 * p1=... T2 * p2= reinterpret...
Started by sinec on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
If T1 is a POD-type and T2 is unsigned char ), the approach with static_cast....
Quoted from the link:
C-style cast is a deprecated feature-reinterpretcast
For types for which such cast is permitted (e.g.
Here is an interesting link ...
|
|
I saw one book on C++ mentioning that navigating inheritance hierarchies using static cast is more efficient than using dynamic cast.
Example:
#include <iostream> #include <typeinfo> using namespace std; class Shape { public: virtual ~Shape...
Started by Ankur on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
That's likely_cast does not require RTTI....
Static_cast would succeed (and lead to undefined behavior, such as an eventual crash).
Dynamic_cast would return NULL if you hadn't done the typeid check and the cast couldn't succeed.
|