casting

Detect Object type then cast it accordingly?

南笙酒味 提交于 2019-12-11 23:51:09
问题 My method takes as input an Object. How do i determine it's type, then cast it accordingly? So for example: binarySearch( Object o ); Inside the binarySearch method, i need a way to determine the type (or class) of Object o. Then i need to cast it with that type. how would i do that??? And more specifically, Object o is a child of a base class (EG SalariedEmp is child of Employee), and i specifically need the base class. EDIT: I figured out how to do what I wanted, which really should be a

Determine Object Type of Array Element Parameter

人走茶凉 提交于 2019-12-11 23:50:02
问题 Put a playground below that shows my issue and it's output. I need to write a method you can pass AnyObject? into, then determine the type of that object. If it's an array, I'll also need to determine it's Element type. This works fine before the method is called, but after I can't get at the types. Specifically, the element type doesn't come back proper due to casting. Playground //: Playground - noun: a place where people can play import UIKit import Foundation extension Array { var

Cast Chars To Int in Template Function

☆樱花仙子☆ 提交于 2019-12-11 23:20:38
问题 I have a C++ template function which prints numbers. It works fine for everything, except when I'm working with data of type char . I'd like char to be printed as int , but if I cast this explicitly in the template function, then I will lose precision on my float types. I'd like to be able to say: template<class T> bob(T a){ cout<<if_char_make_int(a)<<endl; } But I'm not sure how to do this, or if it is possible. Any thoughts? 回答1: template<class T> void bob(T a){ std::cout << typename boost:

vb.net bindingsource filter cast date to string

£可爱£侵袭症+ 提交于 2019-12-11 23:19:04
问题 I'm using the filter method of Binding source in VB.net to filter results in a DataGridView based on the text in a search box. However, the idea of this search, is that it shows a row if any of the cells contain the text. So my filter string ends up looking like this: filter = "ProductId LIKE '%" & searchterm & "%'" & " OR ScanDate like '%" & searchterm & "%'" However, when I try to put the filter in the filter property, it complains, saying that it cannot convert the date column to text for

Array, List, IEnumerable, CustomList class cast to one and iterate threw them

一曲冷凌霜 提交于 2019-12-11 23:14:06
问题 I am trying to figure out a way that tells me if a certain type is an array/list/ienumerable/collection ... I dont care what kind of it is even CustomLists so something like FooList<T> : IList<T> FooList : IList or stuff like that. I kinda hoped that a simple type.IsArray would be enough but sadly this isnt the case. I need a way to check if its one of the above types and then check what the underlying type is, and than cast it to a Indexed based collection, where I can loop through the

Casting items to end of linked list in C

随声附和 提交于 2019-12-11 23:08:30
问题 EDIT*(8:14 PM) - Sorry I corrected my code and made this instead a method so it can be more easily understood. I am not sure how to properly cast a struct when adding to the end of a linked list. Compiling this code gives me an cast warning at the very last line. This may be the reason why the rest of my code does not properly function. For example: #include <stdlib.h> typedef struct { int data; struct node *next; } node; node *HEAD = NULL; node *addNode(int num) { if (HEAD == NULL) { HEAD =

Java: (int)(float)Float.valueOf(s) or Float.valueOf(s).toInt()

北战南征 提交于 2019-12-11 22:32:19
问题 I just bumped into this little problem and I wanted the input of other people on this I was wandering what was the best solution to convert a String to an int (int)(float)Float.valueOf(s) or Float.valueOf(s).toInt() s is a String inputed from a textfield so I can not guarantee that it is necessarily an int my instincts is that the double cast is ugly and should be avoided Your input? 回答1: Your requirements are unclear: If you are expecting an integer and don't want to allow the user to enter

C++: Bypassing strict-aliasing through union, then use __restrict extension

流过昼夜 提交于 2019-12-11 22:05:20
问题 I wonder if it is possible to tailor strict aliasing requirements to specifically designed cases, while still preserving strict aliasing in general or -O2/-O3 optimization respectively. To be more precise, in cases where it is needed, strict aliasing can be bypassed using an anonymous union (as pointed out here and here): #define PTR_CAST(type, x) &(((union {typeof(*x) src; type dst;}*) &(x))->dst) Now I wonder if using __restrict on a pointer obtained by such a cast would re-enable no-alias

warning: ISO C++ forbids variable length array

爱⌒轻易说出口 提交于 2019-12-11 20:46:33
问题 I have a function that takes a pointer to a floating point array. Based on other conditions, I know that pointer is actually pointing to a 2x2 OR 3x3 matrix. (in fact the memory was initially allocated as such, e.g. float M[2][2] ) The important thing is I want to make this determination in the function body, not as the function argument. void calcMatrix( int face, float * matrixReturnAsArray ) { // Here, I would much rather work in natural matrix notation if( is2x2 ) { // ### cast

Two phase implicit casting in c++

折月煮酒 提交于 2019-12-11 20:28:26
问题 Isn't there something in the c++ standard that states I can implicitly cast two time? i.e. in case my function takes object A and I call it with object C I wont get compilation error even if there is no direct cast between C and A but there is a cast from C to B and from B to A? At some point in life I though this code was legal but today I found out I was wrong. class A {}; class B { A m_a; public: operator A () { return m_a; } }; class C { B m_b; public: operator B () { return m_b; } };