casting

Casting string to generic type that is a string

Deadly 提交于 2019-12-18 05:59:08
问题 I'm writing a method to do a intelligent type conversion - using ToString() if the type parameter happens to be a string, otherwise casting but returning null if the cast doesn't work. Basically gets as much information out of v it can without throwing an exception. I check that T is indeed a string before I attempt the cast, but the compiler is still not a fan: Cannot convert type 'string' to 'T' And here's my method: public T? Convert<T>(object v) { if (typeof(T) == typeof(string)) { return

passing argument makes pointer from integer

前提是你 提交于 2019-12-18 05:56:18
问题 I can't find my problem. keeps giving me these errors: "c:2:5: note: expected 'int *' but argument is of type 'int'" "c:28:1: warning: passing argument 1 of 'CountEvenNumbers' makes pointer from integer without a cast [enabled by default]" Here is the code: 1 #include <stdio.h> 2 int CountEvenNumbers(int numbers[], int length); 3 int main(void) 4 { 5 int length; 6 int X;int Z; int Y; int W; 7 X=0;Y=0;Z=0;W=0; 8 printf("Enter list length\n"); 9 scanf("%d",&length); 10 int numbers[length]; 11

SQL cast datetime

给你一囗甜甜゛ 提交于 2019-12-18 05:54:43
问题 In SQL Server 2005, why does: PRINT Cast('' AS datetime) display: Jan 1 1900 12:00AM I would have thought it should be null ? 回答1: The empty string is casted to 0 which is later casted to the era date. Unlike Oracle , SQL Server distinguishes between NULL and an empty string. 回答2: It's because empty string '' is not NULL . If you do: select Cast(null AS datetime) OUTPUT: ----------------------- NULL (1 row(s) affected) CAST and CONVERT (Transact-SQL) When character data that represents only

Java “cannot cast to Comparable” when using TreeMap [duplicate]

穿精又带淫゛_ 提交于 2019-12-18 05:48:11
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Java: SortedMap, TreeMap, Comparable? How to use? I am using the Java JungI graph package and Netbeans 7. I am getting the following error from Java: Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable at java.util.TreeMap.put(TreeMap.java:542) Here is the code associated with the error: SortedMap<MyVertex, Double> vMap = new TreeMap

ClassCastException vs. “cannot cast” compilation error

我怕爱的太早我们不能终老 提交于 2019-12-18 05:44:14
问题 Studying for my OCA Java SE 7 Programmer I exam, so newbie question. I have an example question I do not understand. The following code compiles, but gives a ClassCastException at runtime: interface Roamable { } class Phone { } public class Tablet extends Phone implements Roamable { public static void main(String... args) { Roamable var = (Roamable) new Phone(); } } When I change Roamable var = (Roamable) new Phone(); into Roamable var = (Roamable) new String(); I get a compilation error

Casting a boxed value

北城余情 提交于 2019-12-18 05:43:29
问题 Why can't an int that's been boxed be directly cast to double ? object o = 12; double d = (double)o; That throw an invalid cast exception. Instead it seems it has to first be cast as an int , and then on to double . object o = 12; double d = (double)(int)o; I'm sure the simple answer is "because that's how boxing works" but I'm hoping someone might shed a bit of light here. 回答1: Check out this question from earlier today: Why am I getting InvalidCastException? Unboxing operations only succeed

Casting a boxed value

天涯浪子 提交于 2019-12-18 05:43:19
问题 Why can't an int that's been boxed be directly cast to double ? object o = 12; double d = (double)o; That throw an invalid cast exception. Instead it seems it has to first be cast as an int , and then on to double . object o = 12; double d = (double)(int)o; I'm sure the simple answer is "because that's how boxing works" but I'm hoping someone might shed a bit of light here. 回答1: Check out this question from earlier today: Why am I getting InvalidCastException? Unboxing operations only succeed

C-Style upcast and downcast involving private inheritance

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:55:17
问题 Consider the following piece of code :- class A {}; class B : private A {}; B* bPtr1 = new B; // A* aPtr1 = bPtr1; // error // A* aPtr2 = static_cast<A*>(bPtr1); // error A* aPtr3 = (A*)bPtr1; B* bPtr2 = (B*)aPtr3; The C-style cast discards the private inheritance while both the implicit and static_cast fail (also dynamic_cast ). Why ? If C-style casts are just bit-fiddling, how are C++ casts implemented i.e. how do they know the type of inheritance from memory footprint? After bPtr1 is

Can static_cast throw an exception in C++?

北城余情 提交于 2019-12-18 04:42:16
问题 Is it safe to assume that static_cast will never throw an exception? For an int to Enum cast, an exception is not thrown even if it is invalid. Can I rely on this behavior? This following code works. enum animal { CAT = 1, DOG = 2 }; int y = 10; animal x = static_cast<animal>(y); 回答1: For this particular type of cast (integral to enumeration type) , an exception might be thrown. C++ standard 5.2.9 Static cast [expr.static.cast] paragraph 7 A value of integral or enumeration type can be

java: boolean instanceOf Boolean?

强颜欢笑 提交于 2019-12-18 04:38:11
问题 I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so? public String test(Object value) { if (! (value instanceof Boolean) ) return "invalid"; if (((Boolean) value).booleanValue() == true ) return "yes"; if (((Boolean) value).booleanValue() == false ) return "no"; return "dunno"; } String result = test(true); // will result in "yes" 回答1: