cast

What is the meaning of the type safety warning in certain Java generics casts?

What is the meaning of the Java warning "Type safety: The cast from Object to List is actually checking against the erased type List"? I get it when I try to cast an Object to a type with generic information, such as in the following code: Object object = getMyList();List<Integer> list = (List<Integer>) object;...

Regular cast vs. static_cast vs. dynamic_cast

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 types of casts, and I don't know the difference. What's the difference between th...

C++ cast syntax styles

A question related to Regular cast vs. static_cast vs. dynamic_cast: What cast syntax style do you prefer in C++? C-style cast syntax: (int)foo C++-style cast syntax: static_cast<int>(foo) constructor syntax: int(foo) They may not translate to exactly the same instructions (do they?) but their effect should be the same (right?). If...

long to HWND (VS8 C++)

Hi, How can I cast long to HWND (C++ visual studio 8)? Long lWindowHandler; HWND oHwnd = (HWND)lWindowHandler; But I got the following warning: warning C4312: 'type cast' : conversion from 'LONG' to 'HWND' of greater size Thanks. ...

In C++, why use static_cast<int>(x) instead of (int)x?

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? ...

Is casting the same thing as converting?

In Jesse Liberty's Learning C# book, he says "Objects of one type can be converted into objects of another type. This is called casting." If you investigate the IL generated from the code below, you can clearly see that the casted assignment isn't doing the same thing as the converted assignment. In the former, you can see the boxing/un...

How to emulate C# as-operator in Java

There are situations, where it is practical to have a type-cast return a null value instead of throwing a ClassCastException. C# has the as operator to do this. Is there something equivalent available in Java so you don't have to explicitly check for the ClassCastException? ...

Beginner: Fastest way to cast/copy byte() into single()

I've got a byte() array returned as result of directx sound capture, but for other parts of my program I want to treat the results as single(). Is trundling down the array item by item the fastest way of doing it or is there a clever way to do it ? The code that gets it is CType(Me._applicationBuffer.Read(Me._nextCaptureOffset, GetTyp...

Cast or convert when retrieving data from a database?

When accessing an object in a DataTable retrieved from a database, are there any reasons not to cast the object into your desired type, or are there reasons to use convert? I know the rule is cast when we know what data type we're working with, and convert when attempting to change the data type to something it isn't. Presuming we know w...

Casting an object to a generic interface

I have the following interface: internal interface IRelativeTo<T> where T : IObject { T getRelativeTo(); void setRelativeTo(T relativeTo); } and a bunch of classes that (should) implement it, such as: public class AdminRateShift : IObject, IRelativeTo<AdminRateShift> { AdminRateShift getRelativeTo(); void setRelativeT...

Type Casting in C++

May be the question will be a little bit noobie, but can anybody advice me some books/sites/articles about how and when it's better to use typecasting in C++ style and when it would be better to prefer C-style? I've read a few books (Shildt, Eckel) and haven't found good material on this theme though I think it's one of the strongest C+...

Which variables should I typecast when doing math operations in C/C++?

For example, when I'm dividing two ints and want a float returned, I superstitiously write something like this: int a = 2, b = 3; float c = (float)a / (float)b; If I do not cast a and b to floats, it'll do integer division and return an int. Similarly, if I want to multiply a signed 8-bit number with an unsigned 8-bit number, I will ...

call a Method with parameters got from generic Method

Hi, I have a class storing the name of a WS method to call and the type and value of the only parameter that service receives (it will be a collection of parameters but lets keep it simple for the example): public class MethodCall { public string Method { get; set; } public Type ParType { get; set; } public string ParValue { get; ...

What could cause a dynamic_cast to crash ?

I have a piece of code looking like this : TAxis *axis = 0; if (dynamic_cast<MonitorObjectH1C*>(obj)) axis = (dynamic_cast<MonitorObjectH1C*>(obj))->GetXaxis(); Sometimes it crashes : Thread 1 (Thread -1208658240 (LWP 11400)): #0 0x0019e7a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2 #1 0x048c67fb in __waitpid_nocancel () ...

qt/c++ casting question

I have a QVariant object within a QTreeWidgetItem, how can I cast it to my own object? ...

What is the proper way to cast from an 'OLE_HANDLE' to an 'HICON'?

What is the proper way to cast from an 'OLE_HANDLE' to an 'HICON' for an x64 target build? In particular with a normal C-Style cast, I get this warning when compiling with an x64 config: warning C4312: 'type cast' : conversion from 'OLE_HANDLE' to 'HICON' of greater size Here is the offending code: imgList.Add((HICON)ohIcon); The a...

Primitive type 'short' - casting in Java

Hello, I have a question about the primitive type 'short' in Java. I am using JDK 1.6. If I have the following: short a = 2; short b = 3; short c = a + b; the compiler does not want to compile - it says that it "cannot convert from int to short" and suggests that I make a cast to short, so this: short c = (short) (a + b); real...

PHP String to Float

I am not familiar with PHP at all and had a quick question. I have 2 variables @pricePerUnit and @invoicedUnits. Here's the code that is setting these to values: $InvoicedUnits = ((string) $InvoiceLineItem->InvoicedUnits); $pricePerUnit = ((string) $InvoiceLineItem->PricePerUnit); If I output this, I get the correct values. Lets say ...

Javascript: Cast Math.sqrt to int?

I've searched through google (maybe I didn't look hard enough) but I could not find how to turn Math.sqrt into an int. I want to use Math.sqrt for a for loop and I guess I need it as an int but I can't seem to figure out how to cast the result to an int. So how do I do it? I tried something similar to Java: (int) Math.sqrt(num); Bu...

How can I convert a string to an integer in C++

I am trying to copy the value in bar into the integer foo. This is what I have so far. When I run it I get a different hex value. Any help would be great. int main() { string bar = "0x00EB0C62"; int foo = (int)bar; cout << hex << foo; ChangeMemVal("pinball.exe", (void*) foo, "100000", 4); return 0; } So the ou...