casting

How to make type cast for python custom class

让人想犯罪 __ 提交于 2020-01-13 10:18:08
问题 Consider a class; class temp: def __init__(self): pass I make an object of it; obj = temp() Convert it to string; strObj = str(obj) Now, how can i convert strObj to an object of temp class?? org = temp(strObj) 回答1: As noted by Anand in comments, what you're looking for is object serialization and deserialization. One way to achieve this is through the pickle (or cPickle) module: >>> import pickle >>> class Example(): ... def __init__(self, x): ... self.x = x ... >>> a = Example('foo') >>>

Private inheritance and implicit conversion

浪尽此生 提交于 2020-01-13 09:13:21
问题 I have a class that inherits privately from std::string , and adds some functions. I want to be able to use this class just like std::string , so I am trying to define an implicit conversion operator ( operator string() ). However, I keep getting inaccessible base error. #include <iostream> #include <string> using namespace std; class Test:private string { int _a; public: operator string() { return "hello"; } }; int main() { Test t; if(t == "hello") { cout<<"world\n"; } } Error: trial.cpp: In

Private inheritance and implicit conversion

最后都变了- 提交于 2020-01-13 09:13:16
问题 I have a class that inherits privately from std::string , and adds some functions. I want to be able to use this class just like std::string , so I am trying to define an implicit conversion operator ( operator string() ). However, I keep getting inaccessible base error. #include <iostream> #include <string> using namespace std; class Test:private string { int _a; public: operator string() { return "hello"; } }; int main() { Test t; if(t == "hello") { cout<<"world\n"; } } Error: trial.cpp: In

Fastest way in C# to read block of bytes from file and converting to float[]

给你一囗甜甜゛ 提交于 2020-01-13 02:44:05
问题 I need a fast way in C# leanguage of converting/casting array of bytes encoding one short (int16) value for 2bytes into float representation, as fast as possible. Performance bottleneck was method: samples[sample] = (float)binraryReader.readInt16(); (huge ammount of IO calls so i had to convert to block read) Basically i have file containing block of sound samples (~100-600 mb) of type of short, then, as i can only block read set of bytes, i need to construct short from each pair of bytes and

C# generics: cast generic type to value type

泄露秘密 提交于 2020-01-12 13:49:06
问题 I have a generic class which saves value for the specified type T. The value can be an int, uint, double or float. Now I want to get the bytes of the value to encode it into an specific protocol. Therefore I want to use the method BitConverter.GetBytes() but unfortunately Bitconverter does not support generic types or undefined objects. That is why I want to cast the value and call the specific overload of GetBytes(). My Question: How can I cast a generic value to int, double or float? This

How to convert a LambdaExpression to typed Expression<Func<T, T>>

巧了我就是萌 提交于 2020-01-12 04:24:25
问题 I'm dynamically building linq queries for nHibernate. Due to dependencies, I wanted to cast/retrieve the typed expression at a later time, but I have been unsuccessfull so far. This is not working (the cast is supposed to happen elsewhere): var funcType = typeof (Func<,>).MakeGenericType(entityType, typeof (bool)); var typedExpression = (Func<T, bool>)Expression.Lambda(funcType, itemPredicate, parameter); //Fails This is working: var typedExpression = Expression.Lambda<Func<T, bool>>

C++ Sorting the “percentage” of two paired integer arrays

[亡魂溺海] 提交于 2020-01-11 13:23:09
问题 I have a program with 2 "paired" integer arrays newNumerator[ ], and newDenominator[ ] which both have 9 integers in them. I wrote a function that sorts them in ascending order, however I'm not sure it works as I have not gotten it to successfully compile yet. I am having some issues with typecasting too. Here is the function definition- void sortData(int *newNumerator[], int *newDenominator[], int newSize) { int temp1; int temp2; bool swap; int count = 0; double percentageLeft = 100.0 *

Why a List<type> absorbs not-type element in compilation and execution time?

爷,独闯天下 提交于 2020-01-11 11:35:48
问题 I have this demo, which I dont need a particular solution with a redrawn architecture, but just understanding why behaves like that and any thing I am missing for avoiding it. I am wondering why: Compiler allows the insertion of an element that its not the type of the list into the list The ClassCast exception is thrown when we try to get the element instead of when pushing it import Test.*; //Inner classes import java.util.List; import java.util.ArrayList; public class Test<E extends Object>

Casting a ListBox.SelectedObjectCollection to a ListBox.ObjectCollection?

倾然丶 夕夏残阳落幕 提交于 2020-01-11 11:18:32
问题 Is it possible to cast a ListBox.SelectedObjectCollection to a ListBox.ObjectCollection in C#? If so, how would I go about it? 回答1: I have a function that accepts List<string> . I can pass both SelectedItems and Items by casting them. Try this: SelectedItems.Cast<string>().ToList() Items.Cast<string>().ToList() <string> could be replaced with some other object type. 回答2: This is not possible. Instead, you should use an IList . Both of these types implement IList , so you can pass either one

linq .Cast<> or cast inside ConvertAll for a list

寵の児 提交于 2020-01-11 11:12:51
问题 Consider you have to convert mylist of type List<T> to List<Base> where T is subclass of Base Are these solutions the same? Which has better performances, and why? When should I prefer using the first or the second? return mylist.Cast<Base>().ToList(); return mylist.ConvertAll(x => (Base)x); Maybe the second solution could be better because mylist is converted directly. In the first solution the list is converted to IEnumerable, then to list, but I'm not sure. 回答1: TL;DR: ConvertAll makes 1