casting

Casting data to type of variables stored in object[]

淺唱寂寞╮ 提交于 2020-01-05 04:56:06
问题 I am trying to emulate the way fortran reads data from text files in C#. In fortran you have something like READ(60,2005,ERR=9880,END=8000) A,B,C,D 2005 FORMAT(I8,A20,2F10.3) and A, B, C, D are automatically set to the correct type based on the format statement. I have tried things along the lines of: private static void Example(string[] Data, ref object[] Variables) { for (int i = 0; i < Data.Length; i++) Variables[i] = (typeof(Variables[i]))Data; } but can't even get code that will compile.

Finding a Pointer to NULL

拜拜、爱过 提交于 2020-01-05 04:33:07
问题 I have int* foo[SIZE] and I want to search it for the first element that points to NULL . But when I do this: std::find(foo, foo + SIZE, NULL) I get the error: error C2446: '==' : no conversion from 'const int' to 'int *' Should I just be using static_cast<int*>(NULL) instead of NULL ? C++11 solves this via nullptr but that's not an option for me in C++03 回答1: tl;dr: Use nullptr , or define your own equivalent. The problem is that NULL is some macro that expands to an integral constant

Need clarification of Type Casting operator in Swift

北战南征 提交于 2020-01-05 02:54:05
问题 Why is the type cast operator (as) being used instead of its conditional form (as?) in this switch statement? I thought the type operator could only be (as?) or (as!)...? The Apple Swift documentation does not provide adequate explanation about this. Here is the example in the Swift documentation: var things = [Any]() things.append(0) things.append(0.0) things.append(42) things.append(3.14159) things.append("hello") things.append((3.0, 5.0)) things.append(Movie(name: "Ghostbusters", director:

Objective-C understanding isKindOfClass

被刻印的时光 ゝ 提交于 2020-01-05 02:50:22
问题 Latest version of Objective-C and XCode (4.4). I have a code snippet and I cannot understand why I'm able to use some lines, let me explain : // For understanding purpose : (NSMutableArray*)_programStack id l_topItemOnStack = [_programStack lastObject]; if([l_topItemOnStack isKindOfClass:[NSNumber class]]) { return [l_topItemOnStack doubleValue]; } My question : since my l_topItemOnStack is of type id and I didn't cast it into a NSNumber , how am i able to use the [l_topItemOnStack

REGEXP_SUBSTR converted output is not casting as integer

拜拜、爱过 提交于 2020-01-04 15:52:20
问题 I tried extracting all the digits out of a 20 character string by using REGEXP_SUBSTR Sql function like below. select REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 1) || REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 2) || REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 3) ... ... || REGEXP_SUBSTR(substring(mycolumn,1,20), '^[0-9]', 20) from tbl; But when trying to cast it as bigint / decimal or any numeric data type it is failing with Invalid input syntax for type numeric or Invalid

Unchecked cast warning in Java

情到浓时终转凉″ 提交于 2020-01-04 14:08:28
问题 Eclipse says: Type safety: Unchecked cast from Object to ObjectArrayList<Car> when I do: final ObjectArrayList<Car> icars = (ObjectArrayList<Car>) cars[i]; where cars is defined as: final Object[] cars = new Object[1000]; for (int i = 0; i < 1000; i++) { cars[i] = new ObjectArrayList<Car>(); } Eclipse suggests to add @SuppressWarnings("unchecked") to icars object. But I've read somewhere that annotations are deprecated in Java, so should I leave it as it is? 回答1: The warning is just, well,

How to partition and typecast a List in Kotlin

爱⌒轻易说出口 提交于 2020-01-04 13:35:18
问题 In Kotlin I can: val (specificMembers, regularMembers) = members.partition {it is SpecificMember} However to my knowledge I can not do something like: val (specificMembers as List<SpecificMember>, regularMembers) = members.partition {it is SpecificMember} My question would be - is there's an idiomatic way to partition iterable by class and typecast it those partitioned parts if needed. 回答1: The partition function will return a Pair<List<T>, List<T>> with T being the generic type of your

How to partition and typecast a List in Kotlin

坚强是说给别人听的谎言 提交于 2020-01-04 13:29:10
问题 In Kotlin I can: val (specificMembers, regularMembers) = members.partition {it is SpecificMember} However to my knowledge I can not do something like: val (specificMembers as List<SpecificMember>, regularMembers) = members.partition {it is SpecificMember} My question would be - is there's an idiomatic way to partition iterable by class and typecast it those partitioned parts if needed. 回答1: The partition function will return a Pair<List<T>, List<T>> with T being the generic type of your

java generic casting in generic classes

最后都变了- 提交于 2020-01-04 11:03:27
问题 I'm writing a simple implementation of a generic max heap. If I write public class FastMaxHeap<T>{ T[] data; int size; static final int HEAP_SIZE = 10000; @SuppressWarnings("unchecked") public FastMaxHeap(){ data = (T[]) new Object[HEAP_SIZE]; size = 0; } } it compiles. Now to actually implement the heap, i.e. write maxHeapify(), I need to be able to compare two T's. One option that a priori seems possible would be to tell the compiler that T implements Comparable. But if I type replace < T >

interesting behavior of calling method after casting a subclass to a super class

烈酒焚心 提交于 2020-01-04 07:01:13
问题 class A { int i = 1; int f() { return i; } } class B extends A { int i = 2; int @Override f() { return -i; } } public class override_test { public static void main(String args[]) { B b = new B(); A a = (A) b; // Cast b to an instance of class A. System.out.println(a.i); // Now refers to A.i; prints 1; System.out.println(a.f()); // Still refers to B.f(); prints -2; } } I am wondering why a.f() still refers to B.f() while a.i refers to A.i. thanks in advance! 回答1: Simple rule is when you call a