|
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.
|
|
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.
|
Ask your Facebook Friends
|
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.
|
|
I've created two classes, with one of them having an implicit cast between them:
public class Class1 { public int Test1; } public class Class2 { public int Test2; public static implicit operator Class1(Class2 item) { return new Class1{Test1 = item.Test...
Started by Ryan Versaw on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Because, looking at the code via Reflector, Cast doesnt attempt to take any implicit cast operators (the LINQ Cast code is heavily optimised for special cases of all kinds, but nothing; (Class1)i)) { Console.WriteLine(item.Test1);....
|
|
In postgres I have a table with a varchar column. The data is supposed to be integers and I need it in iteger type in a query. Some values are empty strings. The following:
SELECT myfield::integer FROM mytable
yields ERROR: invalid input syntax for integer...
Started by silviot on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
If the data is supposed to be integers, and you only need... .
SELECT CASE WHEN myfield="" THEN 0 ELSE myfield::integer END FROM mytable
I haven't ever worked with PostgreSQL but I checked the manual for the correct syntax of IF statements in SELECT queries .
|
|
Java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float
Why is this a problem? The numbers that I'm trying to cast are decimals in the domain [-10.0, 10.0]. They start out as Object instances returned using JFormattedTextField.getValue...
Started by CJ on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
You can call doubleValue() on any, then use .substring() to get the ... .
Unlike C++, which will try to find an overloaded cast operator.
Are dealing with primitive types you can just cast from long to float, although it is a 5.1.2 Widening.
|
|
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER VIEW [dbo].[viewProductExportUpdated2] AS SELECT dbo.ProductCategories.ProductCategoryID AS ProductCode, dbo.ProductCategories.ProductCategoryName AS ProductTitle, dbo.Products.ShortDescription AS...
Started by Mike on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
It would be:
CAST(ProdXml /* but the question text says ProductXML.
This is not a CAST to xml.
|