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
3 answers

Casting Between Enums in C#

Possible Duplicate: convert an enum to another type of enum When casting between enums of different types, is it possibly to directly cast from one type to the other like so? LicenseTypes type; TypeOfLicense type2; type2 =…
codewario
  • 19,553
  • 20
  • 90
  • 159
9
votes
3 answers

Java array narrow casting rules

According to JLS 7, 5.1.6 Narrowing Reference Conversion • From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing reference conversion from SC to TC. Object[] objArr =…
Sawyer
  • 15,581
  • 27
  • 88
  • 124
9
votes
2 answers

How to write my own iterator for a collection property of a property (with correct type casting)?

I have a model class with some object compositions, and I don't know the best way to write iterators for this. To see the problem in more details, here is the hierarchy (semi-pseudo code): A Root class: MYEntity : NSObject @property int…
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
9
votes
6 answers

Why is List not a sub-type of List?
public void wahey(List list) {} wahey(new LinkedList()); The call to the method will not type-check. I can't even cast the parameter as follows: wahey((List) new LinkedList()); From my research, I have gathered…
ares
  • 709
  • 6
  • 13
9
votes
6 answers

Cast A primitive type pointer to A structure pointer - Alignment and Padding?

Just 20 minutes age when I answered a question, I come up with an interesting scenario that I'm not sure of the behavior: Let me have an integer array of size n, pointed by intPtr; int* intPtr; and let me also have a struct like this: typedef…
Seçkin Savaşçı
  • 3,446
  • 2
  • 23
  • 39
9
votes
7 answers

casting parent to child - BufferedImage object

I'm get a ClassCastException whenever I try to cast a BufferedImage (parent) to an AdvancedBufferedImage (child) which I extended myself, I've not overridden any methods and I've implemented all the contractors without modifying them I'm gettign…
Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
9
votes
5 answers

Why does "as T" get an error but casting with (T) not get an error?

Why can I do this: public T GetMainContentItem(string moduleKey, string itemKey) { return (T)GetMainContentItem(moduleKey, itemKey); } but not this: public T GetMainContentItem(string moduleKey, string itemKey) { return…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
9
votes
3 answers

C++ automatic type casting : wrong behaviour for a container class

I'm implementing some classes for linear algebra operations on very small constant size vector and matrices. Currenty, when I do : MyMathVector a ={1, 2, 3}; MyMathVector b ={1.3, 2.3, 3.3}; std::cout<<"First =…
Vincent
  • 57,703
  • 61
  • 205
  • 388
9
votes
1 answer

Properly handling the comparison of signed and unsigned values

I arrived at a point where I need to compare singed and unsigned values. Until now I always modified the code base to avoid this situation completely, but now I can't do that. So what is the really proper way to handle singed and unsigned…
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
9
votes
5 answers

Should I really use static_cast every single time I want to convert between primitive types?

What makes this long l = 1; char c = static_cast(l); float f = 1.0f; int i = static_cast(f); better than this long l = 1; char c = (char)l; float f = 1.0f; int i = (int)f; when casting one primitive data type to another? I've got much…
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
9
votes
5 answers

Any type casting done by javac?

To my understanding, if type checking can be done during compilation, the type casting will be done during compilation and will not incur any runtime overhead. For example public Child getChild() { Parent o = new Child(); return (Child)…
qinsoon
  • 1,433
  • 2
  • 15
  • 34
9
votes
2 answers

Casting shared_ptr to shared_ptr

I have a structure: struct Params { std::shared_ptr user_data; /* ... */ }; I want to use it like this: int main() { std::shared_ptr sp(new SpecializedParams(100)); Params params; /* ... */ params.user_data =…
perreal
  • 94,503
  • 21
  • 155
  • 181
9
votes
6 answers

Boxing and unboxing is also casting?

When we convert the data types between primitive data types it is called as data type casting. But when convert between ValueType and ReferenceType we call it as boxing and unboxing. Can boxing and unboxing also be called casting?
LaysomeSmith
  • 661
  • 2
  • 6
  • 14
9
votes
1 answer

Error casting TypeToken to Type when deserializing with gson

I have a class that deserializes an ArrayList of generics with this function just as described in the first answer of this thread: Java abstract class function generic Type public ArrayList arrayType(String data){ return g.fromJson(data, …
dinigo
  • 6,872
  • 4
  • 37
  • 48
9
votes
3 answers

How to cast a 32-bit integer from unsigned to signed in MySQL or PHP?

I have a string in PHP (came from some data source), which represents a formatted unsigned 32-bit integer. I need to store it into a MySQL database as a signed 32-bit integer, so that later I can retrieve it from PHP and use it as a (possibly…
Alex
  • 707
  • 1
  • 8
  • 14
1 2 3
99
100