casting

InvokeExact on the object, whose type is dynamically loaded by classloader

安稳与你 提交于 2019-12-31 01:46:09
问题 I have spend whole day on this problem. My problem is how to make an MethodHandle.invokeExact invocation on an instance, whose class type is dynamically loaded at program runtime. To make problem more clear, i show my sample code below: Class<?> expClass = new MyClassLoader().load(....) //expClass is AddSample.class which is subclass of BaseTemplate BaseTemplate obj = expClass.getConstructor(...) .newInstance(...); MethodHandle myMH = MethodHandles.lookup().findVirtual(expClass, methodName,..

Is it possible to “constify” a field of `std::pair` without hacks?

六月ゝ 毕业季﹏ 提交于 2019-12-31 01:45:28
问题 In C++, the compiling the following code: std::pair <int, int> x; static_cast <std::pair <const int, int>*> (&x); gives an error: error: invalid static_cast from type ‘std::pair<int, int>*’ to type ‘std::pair<const int, int>*’ I more or less understand why it happens, as cv-qualifying a type in a template parameter list can, in principle, give an "incompatible" result. And even if in this case it doesn't, compiler has no way to know it. Anyway, is there a non-hackish way to perform this

Convert MKMapPoint to NSValue in Swift

落花浮王杯 提交于 2019-12-30 22:57:08
问题 I want to convert an MKMapPoint to a NSValue. In Objective-C i can do it with the following statement: MKMapPoint point = MKMapPointForCoordinate(location.coordinate); NSValue *pointValue = [NSValue value:&point withObjCType:@encode(MKMapPoint)]; How can i do that in Swift? Thanks! 回答1: It isn't possible in Swift, but you can still create a category in ObjC and use it in your Swift project // NSValue+MKMapPoint.h @interface NSValue (MKMapPoint) + (NSValue *)valueWithMKMapPoint:(MKMapPoint

Very simple Java Dynamic Casting

人走茶凉 提交于 2019-12-30 20:32:49
问题 Simple question but I have spent over an hour with this. My code is below. I need to make SomeClass sc dynamic. So you pass the class name as a string in a function and use that class in place of static someClass. How to go about it? SomeClass sc; if (someOtherClassObject instanceof SomeClass){ sc=(SomeClass) someOtherClassObject; What I want is public void castDynamic (String strClassName){ //cast the classname referred by strClassName to SomeClass //if it is the instance of SomeClass } EDIT

Introduction To C++ IO Streams

强颜欢笑 提交于 2019-12-30 19:18:06
问题 I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a

Introduction To C++ IO Streams

淺唱寂寞╮ 提交于 2019-12-30 19:16:51
问题 I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a

VB.NET equivalent for C# 7 Type pattern matching

孤街醉人 提交于 2019-12-30 17:14:18
问题 Is there a VB.NET equivalent to this? Note in particular the bmp in the code sample. public void MyMethod(Object obj) { if (obj is Bitmap bmp) { // ... } } Or the short pattern matching syntax with is is exclusive to C#? EDIT: I already know these syntaxes: If TypeOf obj Is Bitmap Then Dim bmp As Bitmap = obj ' ... End If or Dim bmp As Bitmap = TryCast(obj, Bitmap) If bmp IsNot Nothing Then ' ... End If What I want to know is whether there is something even shorter, like that new C#7 syntax..

Why does the compiler complain when I do not cast the result of malloc?

对着背影说爱祢 提交于 2019-12-30 11:24:06
问题 I am inspecting code that does not require explicitly casting result of malloc call but whenever I attempt to do this, the compiler throws an error. i.e. char *somevar; somevar = malloc(sizeof(char) * n); //error somevar = (char *)malloc(sizeof(char) * n); // ok 回答1: This happens if you use C++ compiler instead of C compiler. As C++ requires explicit casting. The problem is not just with (un)casting malloc result, but any void pointer to other pointer. 回答2: Did you remember to include the

Alter character field to date

China☆狼群 提交于 2019-12-30 11:07:25
问题 I've a legacy postgres db that has date columns cast as character(50) fields (don't ask). I'd like to alter the table and columns to contain actual dates. Because this worked: select distinct to_date(date_begin, 'YYYY DD MM') from dates; I naively thought this might work: alter table dates alter column date_begin type character using to_date(date_begin, 'YYYY DD MM'); But it does not. Any clues for the clueless? 回答1: This just works as intended by the OP. What we have here is a simple thinko

Dynamic linq: Is there a way to access object data by index?

本秂侑毒 提交于 2019-12-30 10:29:45
问题 I need some in-memory filtering with Dynamic Linq. My objects have only a indexer: public object this[int index] { } The access to my data is like: object[0], object[1],... So my query is like this: // get FilterText from user at runtime // eg. filterText can be: [0] > 100 and [1] = "wpf" collection.AsQueryable().where(filterText); Is there any way to do this? 回答1: I have some news for you. It is actually possible... BUT! (Yes, there´s a but). I assume you are using the dynamic Linq library.