Questions tagged [casting]

Casting is a process where an object type is explicitly converted into another type if the conversion is allowed. This process might lead to a change in value.

Some algorithms have significant performance differences for different data-types, for example, calculating the N-dimensional median is much is much faster for integers than float type data. Therefore, the use of casting can be important for code performance.

19752 questions
9
votes
5 answers

Using superclass type for subclass instance

I know this question has been asked a lot, but the usual answers are far from satisfying in my view. given the following class hierarchy: class SuperClass{} class SubClass extends SuperClass{} why does people use this pattern to instantiate…
user1545072
9
votes
7 answers

Avoid casting in inherited java-classes

I have a class: class MyClass { public MyClass getParent() { ... } public MyClass[] getChildren() { ... } .... } and a subclass MySubClass extends MyClass { public String getId() { } ... } Everytime I used getChildren() or…
t777
  • 3,099
  • 8
  • 35
  • 53
9
votes
5 answers

cast from List to List

I think I made some mistake while writing my code. I have a class MyClass that implements Interface, now I've a method that is generic for List of Interface as a parameter. I want to use my method by passing a List. I can't cast from List to List…
Pievis
  • 1,954
  • 1
  • 22
  • 42
9
votes
2 answers

How a positive value becomes negative after casting byte in Java?

public class Test1 { public static void main(String[] args) { byte b1=40; byte b=(byte) 128; System.out.println(b1); System.out.println(b); } } the output is 40 -128 the first output is 40 I…
rajeev
  • 499
  • 1
  • 8
  • 17
9
votes
1 answer

casting void** to 2D array of int - C

i am trying to cast a void** pointer to an int** 2D array in C here is the code that i am trying to work with (with all the extraneous bits removed): \*assume that i have a data structure called graph with some *element "void** graph" in it and…
guskenny83
  • 1,321
  • 14
  • 27
9
votes
3 answers

pow() cast to integer, unexpected result

I have some problems using an integer cast for the pow() function in the C programming language. The compiler I'm using is the Tiny C Compiler (tcc version 0.9.24) for the Windows platform. When executing the following code, it outputs the…
Jori
  • 1,122
  • 2
  • 18
  • 36
9
votes
1 answer

How to cast a QChar to int

In C++ there is a way to cast a char to int and get the ascii value in return. Is there such a way to do the same with a qchar? Since unicode supports so many characters and some of them are actually looking alike, it is sometimes hard to tell what…
alexander remus
  • 409
  • 1
  • 4
  • 11
9
votes
2 answers

MySQL decimal fields returned as strings in PHP

By default mysqli returns all values as strings, the MYSQLI_OPT_INT_AND_FLOAT_NATIVE option allows you to convert ints and floats to their appropriate types. Though this does not affect decimal fields. Is there a way to automatically cast all…
lsjroberts
  • 163
  • 2
  • 5
9
votes
2 answers

C# Type Casting at Run-Time Using Reflection

Judging by the title of this question, what I want to do may not be possible so I will describe what I'm doing and you can feel free to let me know what I'm doing wrong and what would be a better way to accomplish my goal. I have an XML file that…
Sanjamal
  • 346
  • 2
  • 6
9
votes
1 answer

iOS: Converting id to int

I have troubles converting id of an object to int (or NSINteger), so that I can use it in a loop later. Here is the case: // "tasks" is a mutable array int taskNo = [[tasks indexOfObject:@"something"] integerValue]; But it results in: Bad receiver…
cell
  • 119
  • 1
  • 1
  • 7
9
votes
1 answer

how to cast an indirect pointer in Objective-C

I am trying to do some work on my keychain and am following this tutorial here Unfortunately I am getting the following error where it talks about searching the keychain Cast of an indirect pointer to an Objective-C pointer to 'CFTypeRef *' (aka…
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
9
votes
3 answers

scala value toInt is not a member of Any

The println in the following code works (with or without toInt) println("retweets : ", e.getOrElse("retweets", 0).toInt) top10Tweets(""+e.get("text").get, e.getOrElse("retweets", 0).toInt) But when I pass it as an argument of a function (as…
user644745
  • 5,673
  • 9
  • 54
  • 80
9
votes
4 answers

Type casting in PHP - security and efficiency

I understand that PHP is a weakly-typed language. My question is: on balance, is it desirable to initialise variables in PHP as specific types, having regard to security and efficiency concerns? Or would this be swimming needlessly against the…
Grant_Bailey
  • 308
  • 1
  • 2
  • 13
9
votes
6 answers

Converting String List into Int List in SQL

I have a nvarchar(MAX) in my stored procedure which contains the list of int values, I did it like this as it is not possible to pass int list to my stored procedure, but, now I am getting problem as my datatype is int and I want to compare the…
Incredible
  • 3,495
  • 8
  • 49
  • 77
9
votes
4 answers

How do I cast `std::string` to `std::vector` without making a copy?

There is a library function I want to call whose signature is: bool WriteBinary(const std::vector & DataToWrite); I have an std::string variable, I want to send it this function as argument. void WriteString(const std::string &…
hkBattousai
  • 10,583
  • 18
  • 76
  • 124