|
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.
|
|
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 .
|
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):
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.
|
|
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.
|
|
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):
From More Effective C++ by Scott Meyers
This operator rule of thumb is: "static....
static_cast would be preferred.
static_cast is always preferable, avoid doing reinterpret_cast s unless absolutely neccessary be used when.
|
|
Why whenever I compile and run the following code in Visual Studio 2008:
double value1 = 10.5; double value2 = 15.5; int whole_number = value1 + value2; Console::WriteLine(whole_number);
I get an incorrect value of 26 while the answer is 25.
However when...
Started by Anthony on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Double value1 = 10.5; double value2 = 15.5; int whole_number = value1 + value2; // int whole_number = 26.0; Console::WriteLine(whole_number);
What would you expect instead? The compiler first evaluates the right side....
It's absolutely right.
|
|
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):
Static cast is also used to cast pointers to related types, for example casting void* to the appropriate, with....
static_cast
static_cast(expression) The static.
Remove const(ness) (or volatile-ness) of a variable.
|
|
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):
static....
Use static_cast for ordinary type conversions.
The difference with static_cast is that a dynamic_cast does runtime checking which may pointers/references within an inheritance hierarchy.
Are used a lot.
|