casting

How to apply overloaded polymorphed function on elements of base class pointer vector

ぐ巨炮叔叔 提交于 2020-04-16 08:15:47
问题 I have a base class Object : struct Object{ }; and n (in this case 2) classes that inherit from this struct Integer : public Object{ int i_; Integer(int i) : i_{i}{} } struct Float : public Object{ float f_; Float(float f) : f_{f}{} } By (ab-)using polymorphism I can now store those two types in a vector: std::vector<Object*> object_list{new Integer(1), new Float(2.1), new Integer(3), new Float(4.2)}; But now I would like to add all those values together. I can think of... 1) ...defining

PHP floating point precision: Is var_dump secretly rounding and how can I debug precisley then?

老子叫甜甜 提交于 2020-04-13 03:58:47
问题 That floating point numbers in PHP are inaccurate is well known (http://php.net/manual/de/language.types.float.php), however I am a bit unsatisfied after the following experiment: var_dump((2.30 * 100)); // float(230) var_dump(round(2.30 * 100)); // float(230) var_dump(ceil(2.30 * 100)); // float(230) var_dump(intval(2.30 * 100)); // int(229) var_dump((int)(2.30 * 100)); // int(229) var_dump(floor(2.30 * 100)); // float(229) The internal representation must be something like 229.999998 . var

What is the correct way to convert 2 bytes to a signed 16-bit integer?

纵然是瞬间 提交于 2020-04-08 18:49:26
问题 In this answer, zwol made this claim: The correct way to convert two bytes of data from an external source into a 16-bit signed integer is with helper functions like this: #include <stdint.h> int16_t be16_to_cpu_signed(const uint8_t data[static 2]) { uint32_t val = (((uint32_t)data[0]) << 8) | (((uint32_t)data[1]) << 0); return ((int32_t) val) - 0x10000u; } int16_t le16_to_cpu_signed(const uint8_t data[static 2]) { uint32_t val = (((uint32_t)data[0]) << 0) | (((uint32_t)data[1]) << 8); return

What is the correct way to convert 2 bytes to a signed 16-bit integer?

三世轮回 提交于 2020-04-08 18:48:32
问题 In this answer, zwol made this claim: The correct way to convert two bytes of data from an external source into a 16-bit signed integer is with helper functions like this: #include <stdint.h> int16_t be16_to_cpu_signed(const uint8_t data[static 2]) { uint32_t val = (((uint32_t)data[0]) << 8) | (((uint32_t)data[1]) << 0); return ((int32_t) val) - 0x10000u; } int16_t le16_to_cpu_signed(const uint8_t data[static 2]) { uint32_t val = (((uint32_t)data[0]) << 0) | (((uint32_t)data[1]) << 8); return

MS SQL Server View not allowing CONCAT

非 Y 不嫁゛ 提交于 2020-04-07 08:00:06
问题 MS SQL Server 2014. I have a SQL statement that works fine: SELECT CONCAT ( CAST(T1.[F1] AS INTEGER), CAST(T1.[F2] AS INTEGER) ) AS F3 FROM mytable AS T1 If I then put this into a view, and try to run I receive the error: Operand data type int is invalid for concat operator F1 and F2 both contain decimals but I want them concatenating e.g.: F1 = 123.00000 F2 = 456.00000 Therefore F3 = 123456 Why does the view not allow this and is there a solution? 回答1: Don't use the visual designers. They

Java: Why we need to cast a float but not a double?

雨燕双飞 提交于 2020-04-07 01:04:14
问题 I don't know if you consider this an important question or not but I want to know. float is a float number (4 bytes). double is a float number (8 bytes). Why we define double wihtout casting: double d = 2.1; But we need to cast with floats: float f = (float) 2.1; or float f = 2.1f; Thank you in advance. 回答1: A floating-point literal is of type float if it ends with the letter F or f ; otherwise its type is double and it can optionally end with the letter D or d . The floating point types (

Conversion from string to generic type

只谈情不闲聊 提交于 2020-03-18 05:46:08
问题 I need to convert string to variable value. I found solution only for C#. I need it in Java. public class Property<T> { T value; public void setValue(String input){ if(value instanceof String){ value= input; // value is type of T not type of string (compilation error) // incompatible types: String cannot be converted to T } if(value instanceof int){ //parse string } if(value instanceof boolean){ //parse string } ... } } 回答1: That is not how it works. You can, however, use polymorphism, to

Casting C# out parameters?

一曲冷凌霜 提交于 2020-03-17 07:44:31
问题 Is it possible to cast out param arguments in C#? I have: Dictionary<string,object> dict; // but I know all values are strings string key, value; Roughly speaking (and if I didn't have static typing) I want to do: dict.TryGetValue(key, out value); but this obviously won't compile because it "cannot convert from 'out string' to 'out object'". The workaround I'm using is: object valueAsObject; dict.TryGetValue(key, out valueAsObject); value = (string) valueAsObject; but that seems rather

casting int to char using C++ style casting [duplicate]

╄→尐↘猪︶ㄣ 提交于 2020-03-17 06:45:06
问题 This question already has answers here : Regular cast vs. static_cast vs. dynamic_cast [duplicate] (8 answers) Why use static_cast<int>(x) instead of (int)x? (9 answers) Closed 6 years ago . In traditional C you can do: int i = 48; char c = (char)i; //Now c holds the value of 48. //(Of course if i > 255 then c will not hold the same value as i). Which of the c++ casting methods (static_cast, reinterpret_cast) is suited for getting this job done? 回答1: You can implicitly convert between

Cast object to interface in TypeScript

别等时光非礼了梦想. 提交于 2020-03-12 11:40:06
问题 I'm trying to make a cast in my code from the body of a request in express (using body-parser middleware) to an interface, but it's not working. It's possible to do this? This is my interface: export interface IToDoDto { description: string; status: boolean; }; This is the code where I'm trying to do the cast: @Post() addToDo(@Response() res, @Request() req) { const toDo: IToDoDto = <IToDoDto> req.body; this.toDoService.addToDo(toDo); return res.status(HttpStatus.CREATED).end(); } And finally