casting

cast list<A*> to list<B*> where B inherits A

不羁的心 提交于 2019-12-18 08:49:15
问题 I got a function void doSomething(list<A*> list1, list<A*> list2) And classes class B : A class C : A Is there a direct way to call my function like void doSomething(list<B*> listOfB, list<C*> listOfC) or do I have to wrap it manually like void doSomething(list<B*> listOfB, list<C*> listOfC) { list<A*> l1; list<A*> l2; for (B* b : listOfB) l1.insert(b); for (C* c : listOfC) l2.insert(c); doSomething(l1, l2); //calling the function taking supertype } I tried unsuccessfully to cast list<B*> to

why explicit type casting required from double to float but not from int to byte?

拥有回忆 提交于 2019-12-18 08:28:30
问题 Consider following statement: byte by = 5; //works fine literal '5' is of type int and small enough to fit into a variable of type byte. Compiler does the implicit type casting here (from int to byte). Now consider following scenario: float fl = 5.5; //compilation error literal '5.5' is of type double, also small enough to fit into a variable of type float. Why do we need to explicitly type cast like this: float fl = (float) 5.5; //works fine Why compiler is not doing the casting for us in

How to create a not null column in a view

混江龙づ霸主 提交于 2019-12-18 07:41:13
问题 Given a table like: CREATE TABLE "MyTable" ( "MyColumn" NUMBER NOT NULL ); I want to create a view like: CREATE VIEW "MyView" AS SELECT CAST("MyColumn" AS BINARY_DOUBLE) AS "MyColumn" FROM "MyTable"; Only where the column "MyColumn" is "NOT NULL". In SQL Server this is pretty straight forward: CREATE VIEW [MyView] AS SELECT ISNULL(CAST([MyColumn] AS Float), 0.0) AS [MyColumn] FROM [MyTable]; However the Oracle equivalent results in a "NULL" column: CREATE VIEW "MyView" AS SELECT NVL(CAST(

C#: Casting '0' to int

大兔子大兔子 提交于 2019-12-18 07:35:25
问题 I saw a code like this: private readonly object[] m_Values = { (int)0, (int)0 }; What's the idea to cast 0 to int? Isn't it int by 'default' ? 回答1: It is not necessary. I'd assume the programmer got bitten before. I'll post it as a puzzler, which overload will be called in this program? using System; class Program { static void Main(string[] args) { Foo.Bar(0); Console.ReadLine(); } } class Foo { public static void Bar(byte arg) { Console.WriteLine("byte overload"); } public static void Bar

Typecasting int to char in printf() in C

ε祈祈猫儿з 提交于 2019-12-18 07:15:13
问题 int *int_pointer = malloc(10); *int_pointer = 53200; printf("The integer at byte #0 is set to: %d \n", (char) *int_pointer); RESULT: -48 So I was just trying this out to see what would happen and I got this really unexpected result. Why -48? How did it even turn into negative? 回答1: Language-lawyer perspective: I believe that correct reference in C99/C11 standard is §6.3.1.3 (with emphasis mine): When a value with integer type is converted to another integer type other than _Bool, if the value

Typecasting int to char in printf() in C

房东的猫 提交于 2019-12-18 07:15:09
问题 int *int_pointer = malloc(10); *int_pointer = 53200; printf("The integer at byte #0 is set to: %d \n", (char) *int_pointer); RESULT: -48 So I was just trying this out to see what would happen and I got this really unexpected result. Why -48? How did it even turn into negative? 回答1: Language-lawyer perspective: I believe that correct reference in C99/C11 standard is §6.3.1.3 (with emphasis mine): When a value with integer type is converted to another integer type other than _Bool, if the value

How expensive is downcasting in Java 6? [closed]

爷,独闯天下 提交于 2019-12-18 07:08:59
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . There is a method which receives an argument of type Collection and it needs to use some of the methods that are found in the List class when it does work with that argument. Is up-casting expensive in terms of speed? List<Stuff> list = (List<Stuff>) collection; I would also like

Need some clarification regarding casting in C

自古美人都是妖i 提交于 2019-12-18 07:05:29
问题 I was just reading about the bad practice of casting the return value of malloc . If I understood correctly, it is absolutely legal to leave the cast as it is done implicitly (and should be left, because of other problems it could generate). Well my question is, when should I then cast my values ? Is there some general rule or something ? For example, this code compiles without any errors with gcc -W -Wall (except unused bar , but that's not the point): float foo(void) { double bar = 4.2;

Why do I have to typecast an int in a ternary expression? [duplicate]

只谈情不闲聊 提交于 2019-12-18 07:02:45
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation and want to know why I have to do it. I'm using .NET 3.5. This works: short foo; if (isValid) foo = -1; else foo = getFoo(); This does not work: short foo; foo = isValid ? -1 : getFoo(); I have to typecast -1: short foo; foo = isValid ? (short)-1 : getFoo(); What does the ternary expression do differently? It considers the -1 to be

Why do I have to typecast an int in a ternary expression? [duplicate]

那年仲夏 提交于 2019-12-18 07:01:45
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation and want to know why I have to do it. I'm using .NET 3.5. This works: short foo; if (isValid) foo = -1; else foo = getFoo(); This does not work: short foo; foo = isValid ? -1 : getFoo(); I have to typecast -1: short foo; foo = isValid ? (short)-1 : getFoo(); What does the ternary expression do differently? It considers the -1 to be