casting

PFObject unable to be cast to custom subclass

时光总嘲笑我的痴心妄想 提交于 2019-12-13 13:03:22
问题 This code was working fine before the recent update to the swift compiler update. I have corrected all the obvious errors, but for some reason when I run the app now it crashes in the for in loop when trying to cast [AnyObject] to my custom Memory class. The log and error shows as follows: Has connectivity Successfully retrieved 8 memories. current memory: <Memories: 0x170120960, objectId: AO3EvPFob4, localId: (null)> { ACL = "<PFACL: 0x174047e30>"; Date = "3915-04-08 05:00:00 +0000"; Guests

Difference Between i = i+1 & i++

北慕城南 提交于 2019-12-13 12:52:07
问题 Can anyone solve my confusion here is my code : byte i = 0; i++; System.out.println(i); Result: 1 byte i = 0; i = i+1; System.out.println(i); Generate compile time error: Type mismatch: cannot convert from int to byte When I convert that to byte like: i = (byte) (i+1); then happily getting result 1 Performing this example i am understand i = i+1 & i++ perform can't same opearation so now i want to know what is exactally difference between them ...!!! 回答1: i++ and i+=1 implicitly cast the

error: cast from 'Foo*' to 'unsigned int' loses precision

点点圈 提交于 2019-12-13 12:50:59
问题 I'm trying to cast a pointer to an int (or unsigned int) and no matter what I try it doesn't want to work. I've tried static_cast<intptr_t>(obj) , reinterpret_cast<intptr_t>(obj) , and various combinations of C style casts, intptr_t 's, unsigned int 's, and I'm including stdint.h. From what I've read, one of the many things I've tried should work. What gives? I didn't bother including the code because it's exactly what I described, but since you asked, I've tried all of these plus other

Check if explicit cast succeed

懵懂的女人 提交于 2019-12-13 12:44:32
问题 I convert a double variable to void pointer : double doub = 3; void *pointer = &doub; If I convert the void pointer to int , not to double : int i = *((int *) pointer); I get : i=0 . How can I check if the cast succeed and the returned value is 0 since the original value is 0, or failed? 回答1: Because C++ isn't dynamically typed language you can't do it straight with void* but you have to use dynamic_cast and some template wrapper: struct type_base { virtual ~type_base() {} template<typename T

Generic Type cast [duplicate]

流过昼夜 提交于 2019-12-13 12:24:08
问题 This question already has answers here : Casting to generic type in Java doesn't raise ClassCastException? (5 answers) Closed 4 years ago . I have the following class (simplified but still a working example): class Test<T> { List<T> l = new ArrayList<>(); public Test() { } public void add(Object o) { l.add((T)o); } } And the test code: Test<Double> t = new Test<>(); t.add(1); t.add(1.2); t.add(-5.6e-2); t.add("hello"); Everything is working fine, and that's not what I was expecting. Shouldn't

How does one safely static_cast between unsigned int and int?

我是研究僧i 提交于 2019-12-13 11:56:23
问题 I have an 8-character string representing a hexadecimal number and I need to convert it to an int . This conversion has to preserve the bit pattern for strings "80000000" and higher, i.e., those numbers should come out negative. Unfortunately, the naive solution: int hex_str_to_int(const string hexStr) { stringstream strm; strm << hex << hexStr; unsigned int val = 0; strm >> val; return static_cast<int>(val); } doesn't work for my compiler if val > MAX_INT (the returned value is 0). Changing

Any differences between asInstanceOf[X] and toX for value types?

怎甘沉沦 提交于 2019-12-13 11:54:05
问题 I used IntelliJ's ability to convert Java code to Scala code which generally works quite well. It seems that IntelliJ replaced all casts with calls to asInstanceOf . Is there any valid usage of asInstanceOf[Int] , asInstanceOf[Long] etc. for value types which can't be replaced by toInt , toLong , ...? 回答1: I do not know of any such cases. You can check yourself that the emitted bytecode is the same by compiling a class like class Conv { def b(i: Int) = i.toByte def B(i: Int) = i.asInstanceOf

Cast element in Java For Each statement

∥☆過路亽.° 提交于 2019-12-13 11:52:41
问题 Is it possible (or even advisable) to cast the element retrieved from a for each statement in the statement itself? I do know that each element in list will be of type <SubType> . I.E.: List<BaseType> list = DAO.getList(); for(<SubType> element : list){ // Cannot convert from element type <BaseType> to <SubType> ... } rather than: List <BaseType> list = DAO.getList(); for(<BaseType> el : list){ <SubType> element = (<SubType>)el; ... } 回答1: Do you really know that each entry is going to be a

What harsh examples are there showing C-style casts are bad?

▼魔方 西西 提交于 2019-12-13 11:49:34
问题 Recently I found a great example of why C-style casts are bad. We start with a following class implementing multiple COM interfaces (I have two for brevity, but there can be ten in real life): class CMyClassInitial : public IInterface1, public IInterface2 { //declarations omitted }; HRESULT CMyClassInitial::QueryInterface(REFIID iid, void** ppv) { if( ppv == 0 ) { return E_POINTER; } *ppv = 0; if( iid == __uuidof(IUnknown) || iid == __uuidof(IInterface1) ) { *ppv = (IInterface1*)this; } else

Cannot pass a List<Foo> to a method expecting a List<IFoo>, where Foo : IFoo

大憨熊 提交于 2019-12-13 11:36:28
问题 I have a class Foo implementing the IFoo interface. I have a method taking a List<IFoo> as a parameter. However, it cannot convert from List<Foo> to List<IFoo> - this surprises me, since Foo implements the IFoo interface. How can I get around this, and why does this occur? (Always good to learn from mistakes) 回答1: This is because List<T> is not covariant. For details, see Covariance and Contravariance in C#. If you can make your method work on IEnumerable<T> instead, this will work (you can