casting

Standard behavior for direct initialization of unsigned short

こ雲淡風輕ζ 提交于 2019-12-23 06:54:52
问题 I noticed today that in the example code: void print(unsigned short a) { std::cout << a << std::endl; } Initialization and use works like this: print(short (5)); But not like this: print(unsigned short(6)); main.cpp:16:8: error: expected primary-expression before 'unsigned' print(unsigned short(6)); And it's not to do with the type since this also works: typedef unsigned short ushort; print(ushort (6)); Live example. So I went searching for what the standard says about value initialization.

Standard behavior for direct initialization of unsigned short

牧云@^-^@ 提交于 2019-12-23 06:54:47
问题 I noticed today that in the example code: void print(unsigned short a) { std::cout << a << std::endl; } Initialization and use works like this: print(short (5)); But not like this: print(unsigned short(6)); main.cpp:16:8: error: expected primary-expression before 'unsigned' print(unsigned short(6)); And it's not to do with the type since this also works: typedef unsigned short ushort; print(ushort (6)); Live example. So I went searching for what the standard says about value initialization.

Casting null as an object?

懵懂的女人 提交于 2019-12-23 06:48:05
问题 I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with it or no? 回答1: Sometimes, you need to to this when the method is overloaded... to tell the compiler which one you are calling. A null object is still null and it is safe. 回答2: it probably needs the cast to resolve overloads 来源: https://stackoverflow.com/questions/358079/casting-null-as-an-object

Smartcast is impossible because propery has open or custom getter

僤鯓⒐⒋嵵緔 提交于 2019-12-23 06:47:26
问题 I am learning Kotlin. My code is as follows: override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) decoupler.attachNotifier(this) if(activity is ScreenRouter) { decoupler.attachRouter(activity) } } attachRouter() method: fun attachRouter(router: ScreenRouter?) { this.router = router } As written in documentation, kotlin automatically casts to type after checking with is operator. So, I expected that it would work. But instead it

Explicit type casting operator in C/C++

回眸只為那壹抹淺笑 提交于 2019-12-23 06:43:48
问题 The following code converts the float type 7.5 to an integer value 7 , the remainder is lost. Here, the typecasting operator is int . I know it is valid typecast in C++. int main() { int i; float f = 7.5; i = (int) f; // Valid in C/C++ } But another way to do the same thing in C/C++ is to use the functional notation preceding the expression to be converted by the type and enclosing the expression between parentheses: i = int (f); // It is worked in C++ but not C So, I have a question, Is it

Explicit type casting operator in C/C++

我与影子孤独终老i 提交于 2019-12-23 06:41:01
问题 The following code converts the float type 7.5 to an integer value 7 , the remainder is lost. Here, the typecasting operator is int . I know it is valid typecast in C++. int main() { int i; float f = 7.5; i = (int) f; // Valid in C/C++ } But another way to do the same thing in C/C++ is to use the functional notation preceding the expression to be converted by the type and enclosing the expression between parentheses: i = int (f); // It is worked in C++ but not C So, I have a question, Is it

Value change when converting a long to a double and back

心已入冬 提交于 2019-12-23 05:49:35
问题 given the following code: long l = 1234567890123; double d = (double) l; is the following expression guaranteed to be true? l == (long) d I should think no, because as numbers get larger, the gaps between two doubles grow beyond 1 and therefore the conversion back yields a different long value. In case the conversion does not take the value that's greater than the long value, this might also happen earlier. Is there a definitive answer to that? 回答1: Nope, absolutely not. There are plenty of

Casting void pointers, depending on data (C++)

喜夏-厌秋 提交于 2019-12-23 05:47:11
问题 Basically what I want to do is, depending on the some variable, to cast a void pointer into a different datatype. For example (the 'cast' variable is just something in order to get my point across): void* ptr = some data; int temp = some data; int i = 0; ... if(temp == 32) cast = (uint32*) else if(temp == 16) cast = (uint16*) else cast = (uint8*) i = someArray[*((cast)ptr)]; Is there anything in C++ that can do something like this (since you can't actually assign a variable to be just (uint32

When is typecasting a pointer allowed?

試著忘記壹切 提交于 2019-12-23 05:36:07
问题 I'm studying for an exam, and one of the sample questions I've been given is the following: "Which of the following expressions when assigned to new_val (the expression will replace HERE ) will print 7 ?" Choose all that apply: float m = 7.0 int *a = (int *) &m; int new_val = HERE; printf("%d\n", new_val); (a) *(int *) &m (b) *a (c) (int) m (d) None of the above. The answer is (c) only. I've run the program on my own machine, and what I found agrees with the answer. However, I was wondering

Cast a variable to a type represented by another Type variable?

岁酱吖の 提交于 2019-12-23 05:09:32
问题 I'm aware that questions like this have been asked before and I doubt it's possible, but I just wanted to make 100% sure that it isn't. In VB.net or C# (either language, it doesn't matter), I want to cast a variable to a type represented by another Type variable. Here's an example of the kind of code that would be needed in C#: Object castMe = new Foo(); Type castTo = typeof(Foo); Foo beenCast = (castTo.GetRepresentedType())castMe; beenCast.MethodInFoo(); ... or in VB, something like: Dim