casting

c++ multiple inheritance casting

风流意气都作罢 提交于 2020-01-01 04:09:26
问题 I have a class: class Base; Also I have an interface class Interface; Next i'm creating a class class Derived : public Base, public Interface; If I have Base *object = new Derived; How can i cast object to Interface ? (of course if i know than object is actually a derived class) EDIT: I've tried dynamic_cast and static_cast (not compiled). So let me explain the problem a bit more: I have: class Object {...} class ITouchResponder { public: virtual bool onTouchBegan(XTouch *touch) = 0; virtual

Is it safe to type-cast TArray<X> to array of X?

江枫思渺然 提交于 2020-01-01 04:08:11
问题 Today I discovered a compiler bug (QC#108577). The following program fails to compile: program Project1; {$APPTYPE CONSOLE} procedure P(M: TArray<TArray<Integer>>); begin SetLength(M, 1, 2); end; begin end. The compiler gags on the SetLength line and says: [dcc32 Error] E2029 ')' expected but ',' found I know I could fix it like this: procedure P(M: TArray<TArray<Integer>>); var i: Integer; begin SetLength(M, 1); for i := low(M) to high(M) do SetLength(M[i], 2); end; but naturally I'm keen to

Is it safe to type-cast TArray<X> to array of X?

≯℡__Kan透↙ 提交于 2020-01-01 04:08:11
问题 Today I discovered a compiler bug (QC#108577). The following program fails to compile: program Project1; {$APPTYPE CONSOLE} procedure P(M: TArray<TArray<Integer>>); begin SetLength(M, 1, 2); end; begin end. The compiler gags on the SetLength line and says: [dcc32 Error] E2029 ')' expected but ',' found I know I could fix it like this: procedure P(M: TArray<TArray<Integer>>); var i: Integer; begin SetLength(M, 1); for i := low(M) to high(M) do SetLength(M[i], 2); end; but naturally I'm keen to

C convert floating point to int

你。 提交于 2020-01-01 04:03:30
问题 I'm using C (not C++). I need to convert a float number into an int . I do not want to round to the the nearest number, I simply want to eliminate what is after the integer part. Something like 4.9 -> 4 .9 -> 4 回答1: my_var = (int)my_var; As simple as that. Basically you don't need it if the variable is int. 回答2: Use in C int C = var_in_float; They will convert implicit Thank you 回答3: If you want to round it to lower, just cast it. float my_float = 42.8f; int my_int; my_int = (int)my_float; //

(void*) casting- what is this used for?

余生长醉 提交于 2020-01-01 03:20:07
问题 I did try searching for this on SO but I think due to the syntax and not knowing exactly what to search I became a little unstuck. I have seen (void*) being used to cast, usually to function calls. What is this used for? 回答1: void* , usually referred to as a void pointer , is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful

Is it possible trigger a compiler / linker error if a template has not been instantiated with a certain type?

不羁的心 提交于 2020-01-01 01:30:46
问题 Follow-up question to [Does casting to a pointer to a template instantiate that template?]. The question is just as the title says, with the rest of the question being constraints and usage examples of the class template, aswell as my tries to achieve the goal. An important constraint: The user instantiates the template by subclassing my class template (and not through explicitly instantiating it like in my tries below). As such, it is important to me that, if possible, the user doesn't need

How to cast String to byte array?

房东的猫 提交于 2019-12-31 07:57:07
问题 I have a String, which contains the byte array's String value. How could I convert this String to byte array? How I tried: String stringValue="33321232"; //the bytes in String byte[] bytes= (byte[])stringValue; System.out.println(getByteArrayAsString(bytes)); The getByteArrayAsString method should give back the result String: 33321232 , so the same which is the stringValue . (This is my method, it is works, but how to get the bytes ?) Thank you! 回答1: I have a String, which contains the byte

How to save/load BigInteger array

早过忘川 提交于 2019-12-31 07:34:09
问题 I want to save/load a BigInteger array into/from the SharedPreferences. How can it be done? For example for the following array: private BigInteger[] dataCreatedTimes = new BigInteger[20]; 回答1: Using Gson you can convert to a json String and back, which then of course makes it trivial to save in preferences: import com.google.gson.Gson; import java.math.BigInteger; public final class BigIntegerArrayJson { private BigIntegerArrayJson(){} public static String toJson(BigInteger[] array) { return

Assignment makes pointer without a cast

◇◆丶佛笑我妖孽 提交于 2019-12-31 07:33:48
问题 I am editing a quick sort code so that the values of low, high, and middle point to an array element instead of integers. This is my code: #include <stdio.h> #define N 10 void quicksort(int a[], int *low, int *high); int split(int a[], int *low, int *high); int main(void) { int a[N], i; printf("Enter %d numbers to be sorted: ", N); for (i=0; i<N; i++) scanf("%d", &a[i]); quicksort(a, &a[0], &a[N-1]); printf("In sorted order: "); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); return 0;

Assignment makes pointer without a cast

拥有回忆 提交于 2019-12-31 07:33:12
问题 I am editing a quick sort code so that the values of low, high, and middle point to an array element instead of integers. This is my code: #include <stdio.h> #define N 10 void quicksort(int a[], int *low, int *high); int split(int a[], int *low, int *high); int main(void) { int a[N], i; printf("Enter %d numbers to be sorted: ", N); for (i=0; i<N; i++) scanf("%d", &a[i]); quicksort(a, &a[0], &a[N-1]); printf("In sorted order: "); for (i=0; i<N; i++) printf("%d ", a[i]); printf("\n"); return 0;