casting

How to cast a value from one enum to another in Java?

别来无恙 提交于 2021-01-21 02:30:28
问题 How can I cast a value from Enum1 to Enum 2 in Java? Here is an example of what I'm trying to do : public enum Enum1 { ONE, TWO, THREE; } public enum Enum2 { FOUR, FIVE, SIX; } So I want to do something like this: Enum2 en2 = (Enum2)ONE; Is it possible and how can I do that? Thanks in advance! 回答1: You cannot cast from one enum to another, however each enum has guaranteed order, and you can easily translate one enum to another (preserving order). For example: enum E1 { ONE, TWO, THREE, } enum

PHP Cast to my class

送分小仙女□ 提交于 2021-01-18 07:39:49
问题 why this is not possible: $user = (User) $u[0]; but this is possible $bool = (boolean) $res['success']; I use PHP 7.0. 回答1: As I know, in PHP you can only cast to some types: (int), (integer) - cast to integer (bool), (boolean) - cast to boolean (float), (double), (real) - cast to float (string) - cast to string (binary) - cast to binary string (PHP 6) (array) - cast to array (object) - cast to object (unset) - cast to NULL (PHP 5) (depracted in PHP 7.2) (marked for remove in 8.0) (see Type

casting does not work on nested list object type and returns empty lists (List<List<Integer>>)

[亡魂溺海] 提交于 2021-01-05 07:01:26
问题 I'm doing some leetcode challenges related to backtracking, namely: https://leetcode.com/problems/permutations/ Where I need to return List<List<Integer>> as the type, however my List<List<Integer>> only gets populated correctly if I accept List<Integer> as my parameter and I cast it to ArrayList<Integer> while I add it in the main result. Code : List<List<Integer>> result = new ArrayList<>(); public List<List<Integer>> permute(int[] nums) { bt(new ArrayList<Integer>(), nums); return result;

Why is type punning considered UB?

可紊 提交于 2021-01-04 03:16:05
问题 Imagine this: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); I have read that type puns like that are undefined behavior. Why? I am literally, reinterpreting 8 bytes of bytes into an 8 byte integer. I don't see how that's different from a union except the type pun being undefined behavior and union s not being? I asked a fellow programmer in person and they said that if you're doing it, either you know what you're doing very well , or you're making a

Why is type punning considered UB?

穿精又带淫゛_ 提交于 2021-01-04 03:14:25
问题 Imagine this: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); I have read that type puns like that are undefined behavior. Why? I am literally, reinterpreting 8 bytes of bytes into an 8 byte integer. I don't see how that's different from a union except the type pun being undefined behavior and union s not being? I asked a fellow programmer in person and they said that if you're doing it, either you know what you're doing very well , or you're making a

Why is type punning considered UB?

别等时光非礼了梦想. 提交于 2021-01-04 03:13:25
问题 Imagine this: uint64_t x = *(uint64_t *)((unsigned char[8]){'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}); I have read that type puns like that are undefined behavior. Why? I am literally, reinterpreting 8 bytes of bytes into an 8 byte integer. I don't see how that's different from a union except the type pun being undefined behavior and union s not being? I asked a fellow programmer in person and they said that if you're doing it, either you know what you're doing very well , or you're making a

What is the correct way to cast a struct to a type that matches its first set of members?

本秂侑毒 提交于 2021-01-01 13:25:19
问题 Please consider the following structs from the WinAPI: typedef struct _PROCESS_MEMORY_COUNTERS { DWORD cb; DWORD PageFaultCount; SIZE_T PeakWorkingSetSize; SIZE_T WorkingSetSize; SIZE_T QuotaPeakPagedPoolUsage; SIZE_T QuotaPagedPoolUsage; SIZE_T QuotaPeakNonPagedPoolUsage; SIZE_T QuotaNonPagedPoolUsage; SIZE_T PagefileUsage; SIZE_T PeakPagefileUsage; } PROCESS_MEMORY_COUNTERS; typedef struct _PROCESS_MEMORY_COUNTERS_EX { DWORD cb; DWORD PageFaultCount; SIZE_T PeakWorkingSetSize; SIZE_T

What is the correct way to cast a struct to a type that matches its first set of members?

扶醉桌前 提交于 2021-01-01 13:11:08
问题 Please consider the following structs from the WinAPI: typedef struct _PROCESS_MEMORY_COUNTERS { DWORD cb; DWORD PageFaultCount; SIZE_T PeakWorkingSetSize; SIZE_T WorkingSetSize; SIZE_T QuotaPeakPagedPoolUsage; SIZE_T QuotaPagedPoolUsage; SIZE_T QuotaPeakNonPagedPoolUsage; SIZE_T QuotaNonPagedPoolUsage; SIZE_T PagefileUsage; SIZE_T PeakPagefileUsage; } PROCESS_MEMORY_COUNTERS; typedef struct _PROCESS_MEMORY_COUNTERS_EX { DWORD cb; DWORD PageFaultCount; SIZE_T PeakWorkingSetSize; SIZE_T

What is the correct way to cast a struct to a type that matches its first set of members?

走远了吗. 提交于 2021-01-01 13:11:08
问题 Please consider the following structs from the WinAPI: typedef struct _PROCESS_MEMORY_COUNTERS { DWORD cb; DWORD PageFaultCount; SIZE_T PeakWorkingSetSize; SIZE_T WorkingSetSize; SIZE_T QuotaPeakPagedPoolUsage; SIZE_T QuotaPagedPoolUsage; SIZE_T QuotaPeakNonPagedPoolUsage; SIZE_T QuotaNonPagedPoolUsage; SIZE_T PagefileUsage; SIZE_T PeakPagefileUsage; } PROCESS_MEMORY_COUNTERS; typedef struct _PROCESS_MEMORY_COUNTERS_EX { DWORD cb; DWORD PageFaultCount; SIZE_T PeakWorkingSetSize; SIZE_T

“redundant cast to java.lang.Object” warning for necessary cast

一笑奈何 提交于 2020-12-28 06:51:45
问题 Consider this Minimal, Reproducible Example : interface Code { static void main(String[] args) { symbol( String.valueOf( true ? 'a' : true ? 'b' : true ? 'c' : fail() ) ); } private static void symbol(String symbol) { System.out.println(symbol); } private static <R> R fail() { throw null; } } (Being near minimal, true is a stand in for a useful boolean expression. We can ignore beyond the first ? : (in the real code, there are lots).) This 'obviously' gives the error. 4: reference to valueOf