casting

Is it legal/safe to cast away `const` for a heap-allocated object?

旧街凉风 提交于 2020-01-30 02:31:38
问题 My use case is as follows. I develop a library in which some loaded plugins can create objects (allocated using malloc() by the library), and some other plugins can read properties of those objects but not modify them. For me this is a case of having a non- const API for the creating/writer side and a const API for the reader side, for example: // writer API struct obj *obj_create(void); void obj_set_some_property(struct obj *obj, int property); // reader API int obj_get_some_property(const

Is it legal/safe to cast away `const` for a heap-allocated object?

淺唱寂寞╮ 提交于 2020-01-30 02:30:11
问题 My use case is as follows. I develop a library in which some loaded plugins can create objects (allocated using malloc() by the library), and some other plugins can read properties of those objects but not modify them. For me this is a case of having a non- const API for the creating/writer side and a const API for the reader side, for example: // writer API struct obj *obj_create(void); void obj_set_some_property(struct obj *obj, int property); // reader API int obj_get_some_property(const

Why is (long)9223372036854665200d giving me 9223372036854665216?

前提是你 提交于 2020-01-29 11:35:21
问题 I know about weird stuff with precision errors, but I can't fathom, Why is (long)9223372036854665200d giving me 9223372036854665216 ? 回答1: 9223372036854665200d is a constant of type double . However, 9223372036854665200 does not fit in a double without loss of precision. A double only has 52 bits of mantissa, whereas the number in question requires 63 bits to be represented exactly. The nearest double to 9223372036854665200d is the number whose mantissa equals 1

Why is (long)9223372036854665200d giving me 9223372036854665216?

妖精的绣舞 提交于 2020-01-29 11:35:06
问题 I know about weird stuff with precision errors, but I can't fathom, Why is (long)9223372036854665200d giving me 9223372036854665216 ? 回答1: 9223372036854665200d is a constant of type double . However, 9223372036854665200 does not fit in a double without loss of precision. A double only has 52 bits of mantissa, whereas the number in question requires 63 bits to be represented exactly. The nearest double to 9223372036854665200d is the number whose mantissa equals 1

How can Flow be forced to cast a value to another type?

谁说我不能喝 提交于 2020-01-29 04:16:16
问题 Is it possible to forcibly cast a variable in Flow? type StringOrNumber = string | number const foo: StringOrNumber = 'hello' // I look for something like `const bar:string = (string) foo` const bar: string = foo // fails const bar: string = (foo: string) // also fails 回答1: Flow doesn't do direct casting from one type to another, but you can do something like const bar: string = (foo: any); so you cast foo to an any , because any accepts any type of value as an input. Then because the any

Convert numeric result to percentage with decimal digits

蹲街弑〆低调 提交于 2020-01-25 20:17:31
问题 I have this query: Select count(incidentnumber) as average from incidents Where IncidentStationGround <> firstpumparriving_deployedfromstation; I got a result, it's something like 20,000. But how can I convert this number to a percentage? Plus, I want the results in decimal, can I? 回答1: your query in comment should work cast count to decimal to achieve decimal percentage count(incidentnumber)::decimal*100 回答2: Assuming percentage of the total count: SELECT (count(incidentnumber) FILTER (WHERE

How to cast type safety a Object to JComboBox<String>?

断了今生、忘了曾经 提交于 2020-01-25 10:13:05
问题 I have this code: @Override public void itemStateChanged(ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) { Object sourceObject = evt.getSource(); if (sourceObject instanceof JComboBox<?>) { JComboBox<String> jComboBox = (JComboBox<String>) sourceObject; } } } What is the best and correct casting for generics type safety and avoiding suppress the warning? 回答1: You nailed it. Runtime instanceof checks don't check generic parameters so the way you have shown it is the standard

How to cast type safety a Object to JComboBox<String>?

二次信任 提交于 2020-01-25 10:12:27
问题 I have this code: @Override public void itemStateChanged(ItemEvent evt) { if (evt.getStateChange() == ItemEvent.SELECTED) { Object sourceObject = evt.getSource(); if (sourceObject instanceof JComboBox<?>) { JComboBox<String> jComboBox = (JComboBox<String>) sourceObject; } } } What is the best and correct casting for generics type safety and avoiding suppress the warning? 回答1: You nailed it. Runtime instanceof checks don't check generic parameters so the way you have shown it is the standard

Shifting bytes and remaining values casted to int (interested in two's complement)

徘徊边缘 提交于 2020-01-25 07:15:09
问题 Why does the code that you see below give these results? void loop() { byte test = 00000101; Serial.print("The byte test is: "); Serial.println(test); int x = (int)test; int y = (int)test<<8; Serial.print("The int x is: "); Serial.println(x); Serial.print("The int shifted y is: "); Serial.println(y); byte tost = 10000001; int z = test + tost; Serial.print("The int z is: "); Serial.println(z); delay(1000); } Results : The byte test is: 65 The int x is: 65 The int shifted y is: 16640 The int z

Placing different generic types into a data structure, and then handling them upon removal

元气小坏坏 提交于 2020-01-25 06:59:42
问题 I posted this question yesterday but it was marked as a duplicate of C# - Multiple generic types in one list To clarify, the answer in that problem shows how to correctly place multiple generic types into a single list. However that's not my problem. My problem is that, upon removing items from the list, I need to know what type they are in order to handle them correctly. As a starting point, I am using the answer to the above question because it does have a good solution for placing items of