casting

c++ reinterpret_cast, virtual, and templates ok?

这一生的挚爱 提交于 2020-01-15 08:32:27
问题 In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public BaseClass { }; Further assume factory classes for these two classes with a common, templated base class: template<typename T> class Factory { public: virtual T* create() = 0; }; class BaseClassFactory : public Factory<BaseClass> { public: virtual BaseClass* create() { return new BaseClass(&m_field); } private: SomeClass m_field; }; class ChildClassFactory : public Factory<ChildClass> { public: virtual

Java 6 - Creating and detecting the first double value above Long.MAX_VALUE

六眼飞鱼酱① 提交于 2020-01-15 03:28:07
问题 Below I attempt to assign to value the maximum Long value, then add to it the minimum positive Double value possible. I then try to detect that the value is greater than the maximum Long value. Double value = new Long(Long.MAX_VALUE).doubleValue(); value += Double.MIN_VALUE; if (value < -(new Long(Long.MAX_VALUE).doubleValue()) || value > new Long(Long.MAX_VALUE).doubleValue()) { // Expecting code here to execute, but it doesn't. } Studying the values involved shows that value has the final

Why is char** (or any T**) to void** cast invalid?

拥有回忆 提交于 2020-01-14 22:57:23
问题 In the first comment to Python C Module - Malloc fails in specific version of Python, @user694733 mentions that casting char** to void** is not valid. I read Invalid conversion from Foo** to void** - why is implicit type conversion allowed to void* but not to void**? and http://c-faq.com/ptrs/genericpp.html but there is a reference to standard, but no real example, in which case this might be incorrect, leading to errores . Thinking of e.g. void** to double** or vice versa, is there a case

How to concatenate all results from table row?

喜夏-厌秋 提交于 2020-01-14 18:10:44
问题 Can not find a solution to do something like: SELECT CONCAT_WS(',', ( SELECT * FROM table WHERE id = 1 )) How can I do that in PostgreSQL? 回答1: Quick and dirty: SELECT t::text FROM tbl t WHERE id = 1; t is an alias for the table and not strictly needed. You can use the original table name as well. But if you have a column of the same name it takes precedence. So t represents the row type of the table, which is automatically coerced to text representation on output. I added an explicit cast to

How to concatenate all results from table row?

本小妞迷上赌 提交于 2020-01-14 18:10:30
问题 Can not find a solution to do something like: SELECT CONCAT_WS(',', ( SELECT * FROM table WHERE id = 1 )) How can I do that in PostgreSQL? 回答1: Quick and dirty: SELECT t::text FROM tbl t WHERE id = 1; t is an alias for the table and not strictly needed. You can use the original table name as well. But if you have a column of the same name it takes precedence. So t represents the row type of the table, which is automatically coerced to text representation on output. I added an explicit cast to

Layout for struct prefix

狂风中的少年 提交于 2020-01-14 15:00:14
问题 The C/C++ languages don't reorder struct members in memory and never insert padding before the first member. But if I have 2 structures that start with the same members, can I cast between them if I only access the common members? In other words, is struct layout greedy? My concrete case is casting between VIDEOINFOHEADER and VIDEOINFOHEADER2 回答1: The C++ standard contains the following statement which, however, only applies to unions (9.2 [class.member] paragraph 19): If a standard-layout

Why the operands of an operator needs to be of the same type?

旧城冷巷雨未停 提交于 2020-01-14 12:18:14
问题 If I have something like this: int i = 123; float f = 123.1; if (f > i) {} else {} The i will be promoted to a float and the comparison will become if (float > float) . My question is why does the int variable needs to be promoted to a float in the first place, Is it because its easier for the computer to compare two plain numbers than trying to figure out what the bits of each number represent and then compare that? 回答1: Because "There are no numbers". Computers don't compare numbers, and

Why the operands of an operator needs to be of the same type?

℡╲_俬逩灬. 提交于 2020-01-14 12:18:09
问题 If I have something like this: int i = 123; float f = 123.1; if (f > i) {} else {} The i will be promoted to a float and the comparison will become if (float > float) . My question is why does the int variable needs to be promoted to a float in the first place, Is it because its easier for the computer to compare two plain numbers than trying to figure out what the bits of each number represent and then compare that? 回答1: Because "There are no numbers". Computers don't compare numbers, and

How to apply a mask to a const void* address?

不想你离开。 提交于 2020-01-14 10:19:26
问题 I work on an embedded target and want to define memory pools. Memory addresses are represented as void* . However in a particular case, those addresses are cached and I want to uncache them to get directly the "real" hardware address. I want to define the address of the beginning of memory_area (which is just a marker): #define UNCACHE_MASK 0xABCDEF12UL // Value of the mask to apply extern uint32_t memory_area; // Global, defined somewhere else const void * virtual_address = &memory_area; //

Cast one object to another

你离开我真会死。 提交于 2020-01-14 10:03:45
问题 I have got two user defined CLR objects which are identical and only differ in name, for example this code: public class Person { public string Name { get; set; } public string Address { get; set; } public string LastName { get; set; } public string ETC { get; set; } } public class PersonData { public string Name { get; set; } public string Address { get; set; } public string LastName { get; set; } public string ETC { get; set; } } I know that this can be done by creating an object from