casting

Explicit Type Conversion of sub class object to super class in java

痞子三分冷 提交于 2019-12-12 08:16:00
问题 Consider below code: public class Test{ public static void main(String str[]){ B b = new B(); A a1 = (A)b;//Explicit type conversion A a2 = b; } } class A{} class B extends A{} In the above code are the two line: A a1 = (A)b;//Explicit type conversion A a2 = b; Equivalent? If not then what is the difference between the two and if yes then is there any scenario in java where we need to explicitly convert a sub class object into a super class object ? 回答1: The explicit type casting of the

Casting and dynamic vs static type in Java

泪湿孤枕 提交于 2019-12-12 07:56:06
问题 I'm learning about static vs dynamic types, and I am to the point of understanding it for the most part, but this case still eludes me. If class B extends A , and I have: A x = new B(); Is the following allowed?: B y = x; Or is explicit casting required?: B y = (B) x; Thanks! 回答1: Explicit casting is required , and will succeed . The reason why it's required is because it doesn't always succeed: a variable declared as A x can refer to instances that aren't instanceof B . // Type mismatch:

C# type conversion inconsistent?

孤街浪徒 提交于 2019-12-12 07:39:03
问题 In C#, I cannot implicitly convert a long to an int . long l = 5; int i = l; // CS0266: Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?) This produces said error. And rightly so; if I do that, I am at risk of breaking my data, due to erroneous truncation. If I decide that I know what I am doing, then I can always do an explicit cast, and tell the compiler that it's okay to truncate, I know best. int i = (int)l; // OK However, the same

Is it possible to write auto-cast operator outside a struct?

心已入冬 提交于 2019-12-12 07:34:22
问题 The exact situation is next: I have defined in system API structs CGPoint and CGSize , and I want to be able to write my_point = my_size . I can't modify CGPoint struct, only can write external operator. I can write binary operators ( + , - , ...) but operator= must by declared inside struct. So is there any other solution? 回答1: To make the expression a = b; compile you need to either have an operator= in the type of a that takes an element of the type of b , or a type implicitly convertible

Is `memcpy((void *)dest, src, n)` with a `volatile` array safe?

烈酒焚心 提交于 2019-12-12 07:26:47
问题 I have a buffer that I use for UART, which is declared this way: union Eusart_Buff { uint8_t b8[16]; uint16_t b9[16]; }; struct Eusart_Msg { uint8_t msg_posn; uint8_t msg_len; union Eusart_Buff buff; }; struct Eusart { struct Eusart_Msg tx; struct Eusart_Msg rx; }; extern volatile struct Eusart eusart; And here is the function that fills the buffer (which will be sent using interrupts): void eusart_msg_transmit (uint8_t n, void *msg) { if (!n) return; /* * The end of the previous transmission

Go conversion between struct and byte array

前提是你 提交于 2019-12-12 07:14:56
问题 I am writing a client - server application in Go. I want to perform C-like type casting in Go. E.g. in Go type packet struct { opcode uint16 data [1024]byte } var pkt1 packet ... n, raddr, err := conn.ReadFromUDP(pkt1) // error here Also I want to perform C-like memcpy(), which will allow me to directly map the network byte stream received to a struct. e.g. with above received pkt1 type file_info struct { file_size uint32 // 4 bytes file_name [1020]byte } var file file_info if (pkt1.opcode ==

Why can I use static_cast With void* but not With char*

独自空忆成欢 提交于 2019-12-12 06:34:09
问题 I know that reinterpret_cast is primarily used going to or from a char* . But I was surprised to find that static_cast could do the same with a void* . For example: auto foo "hello world"s; auto temp = static_cast<void*>(&foo); auto bar = static_cast<string*>(temp); What do we gain from using reinterpret_cast and char* over static_cast and void* ? Is it something to do with the strict aliasing problem? 回答1: Generally speaking, static_cast will do cast any two types if one of them can be cast

Argument Type Problems

梦想的初衷 提交于 2019-12-12 06:20:31
问题 Why am I getting the following error d = Atan ( 1_Real32 / 110443_Real32 ) 1 Error: 'x' argument of 'atan' intrinsic at (1) must be REAL or COMPLEX The funny thing is that I get no problems when I use d = Atan ( Real(1,Real32) / Real(110443,Real32) ) 回答1: The value 1_Real32 is an int-literal-constant (R407, Fortran2008 4.4.2.2 5). To get a real value instead you need to change it to 1._Real32 . The presence of the decimal makes it a real-literal-constant (R413, Fortran2008 4.4.2.3 6).

Static cast equivalent in python

流过昼夜 提交于 2019-12-12 06:19:29
问题 My problem is the following: I am using ROS and I am currently trying to read values from a PointCloud of a Kinect sensor. The values are of type PointCloud2 and PointCloud2.data holds the data of the PointCloud and is of type uint8[]. In my project I am currently using python and I have successfully registered and retrieved the data. My issue is that when trying to retrieve them python parses the uint8[] array as strings. The current part of the script where I have the problem is the

using > in case statement causes syntax error

情到浓时终转凉″ 提交于 2019-12-12 06:14:46
问题 Using the below code gives me a incorrect syntax near '>' the data in the Long field looks like this: Review Body 3, Review body 6, Aprentice, I've been going around in cirlces for hours traying to sort this, is it becasue I'm using a text function (Right) in a calc. SELECT TOP 1000 [Id] ,[Short] ,[Long] ,CASE ISNUMERIC(RIGHT( grade.Long , 1 )) WHEN cast(RIGHT( shiftname.Long , 1 )as int) > 4 THEN 'Qualified' ELSE 'Unqualified' END as Grade FROM [RosterPro_Live].[dbo].[lookup_PostGrade] as