|
I've heard that, in C++, the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?
Started by Tommy Herbert on
, 10 posts
by 10 people.
Answer Snippets (Read the full thread at stackoverflow):
static_cast, aside from manipulating pointers);
static_cast means that you can't accidentally const_cast or reinterpret_cast, which is a good for the static_cast keyword....
Objection your honour, asked and answered.
|
|
In both Microsoft VC2005 and g++ compilers, the following results in an error:
On win32 VC2005: *sizeof(wchar_t) is 2*
wchar_t *foo = 0; static_cast<unsigned short *>(foo);
Results in
error C2440: 'static_cast' : cannot convert from 'wchar_t *' ...
Started by VoidPointer on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
What could it do, set there is no conversion via ....
By spec using of static_cast restricted by narrowable types, eg: std::ostream& to std::ofstream for the implementation to provide a static_cast between the pointer types.
|
|
Could somebody please elaborate on the differences?
Started by Evan Fosmark on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
It might be a static_cast (....
The difference is that (int)foo can mean half a dozen different things.
Look at what Stroustrup of the cast.
Further, static_cast is a lot easier to search for or do search/replace on.
|
Ask your Facebook Friends
|
Hi, I feel really silly asking this because I know how to do it 101 ways, but not the way it is defined in the book. (note, I know C++)
So far, we have only gone over the very basics of C++. So basically, we know variables, assignment, and basic casting...
Started by Earlz on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
In C++, when you convert a float to an integral type, the value... .
Just so you know, this is not a problem with your compiler .
Static_cast<int>(n+0.5)
Or static_cast<int>(n >= 0 ? n + 0.5 : n - 0.5) for more proper behavior on negative n .
|
|
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):
Using] int x = int(d); 013A13F7 fld qword ptr [d] 013A13FA call @ILT+215(__ftol2_sse) (13A10DCh) 013A13FF mov dword ptr [x],eax....
Be no difference at all if you compare int() to equivalent functionality of static_cast<int>() .
|
|
Possible Duplicates:
In C++, why use static_cast<int>(x) instead of (int)x?
Regular cast vs. static_cast vs. dynamic_cast
Hello, I just started a C++ class, though I already know a lot of C++. (though haven't used it in a long while)
Well, in our...
Started by Earlz on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
In....
A static cast is simply a conversion from one type things.
There is no static_typecast in C++, but there is a static_cast , used like this:
double m = 14.0_cast , static_cast , const_cast and dynamic_cast .
|
|
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):
However, if you can to a type....
Implicit conversions, explicit conversions and static_cast are all different things.
The primary function of static_cast is to perform an non changing or semantic transformation from one type.
|
|
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):
Use static_cast, it requires RTTI....
static_cast is just (unless you're casting to a reference type -- then it throws a bad_cast exception).
Dynamic cast requires RTTI and does some magic compared to static cast.
|
|
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):
Allows you to say "I'm doing a legal conversion from one type to another" like from int to double.
|
|
Q1. Why does using NULL pointers with static_cast cause crashes while dynamic_cast and reinterpret_cast give a NULL pointer in return?
The problem occurred in a method similar to the one given below:
void A::SetEntity(B* pEntity, int iMyEntityType) { ...
Started by Gayan on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
You can static_cast a null pointer - it will give of pEntity and iMyEntityType....
Consider the following code:
struct B1 {}; struct B2 {}; struct to B1 is NULL then shift gives invalid result .
Then static_cast may shift your pointer.
|