casting

how to implement casting to a private base class in C++

只谈情不闲聊 提交于 2019-12-11 07:28:14
问题 How to implement casting to a private base class in C++? I don't want to use hacks such as adding a friend etc. Defining public casting operator does not work. EDIT : For example I have: class A { //base class } class AX : private A { //a child } class AY : private A { //another specialized child } class B { //base class void do (A a) {//do } } class BX : private B { //a child void do (AX a) { B::do( static_cast <A> (a) ); } } class BY : private B { //another specialized child void do (AY a)

How does NSData(bytes:length:) convert [Byte] to UnsafePointer<Void> in Swift?

泪湿孤枕 提交于 2019-12-11 07:26:50
问题 Playing with raw data in Swift I came across something I don't understand. NSData has a constructor: init(bytes: UnsafePointer<Void>, length: Int) where the first bytes parameter is clearly of UnsafePointer type. However, if I pass [Byte] object to this constructor, not only does the compiler not complain, but it works ok. But if I try to cast [Byte] to UnsafePointer , I fail. How does this work? For example (you can try it in Playgrounds): let buffer: [Byte] = [0x00, 0xff] let data = NSData

Rust Vector of Traits: cast each trait

匆匆过客 提交于 2019-12-11 07:23:38
问题 I have a problem casting a vector of traits into a vector of different traits. Using the approach of Type-casting arrays/vectors in Rust , I basically tried the following: trait ParentTrait {} trait ChildTrait: ParentTrait {} fn main() { let mut children: Vec<Box<ChildTrait>> = vec![]; let parents = children.iter().map(|&e| e as Box<ParentTrait>); } Now this does not compile, it results in error: the trait `core::kinds::Sized` is not implemented for the type `ChildTrait` [...] error: the

TypeScript: How to cast a generic object?

那年仲夏 提交于 2019-12-11 07:21:38
问题 I have the following interfaces: interface AppState { readonly grid : IGridSettings; readonly selected : ISelectedSettings; } interface IGridSettings { readonly extents : number; readonly isXY : boolean; readonly isXZ : boolean; readonly isYZ : boolean; readonly spacing : number; } interface ISelectedSettings { readonly bodyColor : number; readonly colorGlow : number; readonly lineColor : number; readonly selection : number[] | undefined; } interface Action<T = any> { type: T } interface

Proper way to access array members as a different type?

依然范特西╮ 提交于 2019-12-11 07:19:10
问题 I have a large array of uint16_t . Most of its members are uint16_t , but some are int16_t and some uint8_t . How would you handle that? By the way, I tried: Pointers: Used 2 pointers, one int16_t* and the other uint8_t* , both initialized to the start of the array, to access member of the array that are int16_t and uint8_t . (That worked initially, but I ran into problems when later in the program something else changed the value of the pointers, so I don't trust it.) Type definition with a

c# Cast string as IEnumerable<T>

北城余情 提交于 2019-12-11 07:06:02
问题 I have this function: public static IEnumerable<T> UniqueInOrder<T>(IEnumerable<T> iterable) { return iterable.Distinct(); } This works with any IEnumerable except with a string, I must test it with this call: Assert.AreEqual("ABCDAB", UniqueInOrder("AAAABBBCCDAABBB")); The assert fail: Expected is <System.String>, actual is <System.Linq.Enumerable+<DistinctIterator>c__Iterator10`1[System.Char]> Values differ at index [4] I also tried something like: public static IEnumerable<T> UniqueInOrder

C# cast array to element type

一世执手 提交于 2019-12-11 06:43:56
问题 I have a generic argument T which is an array in one particular case. Is it possible to cast array of objects to the array of typeof(T).GetElementType() ? For example: public TResult Execute<TResult>()// MyClass[] in this particular case { var myArray = new List<object>() { ... }; //actual type of those objects is MyClass Type entityType = typeof(TResult).GetElementType(); //MyClass //casting to myArray to array of entityType TResult result = ...; return result; } 回答1: This is not a good idea

How do I use reflection to properly cast an unknown property to a generic list that derives from a common base?

亡梦爱人 提交于 2019-12-11 06:38:32
问题 Say I have the following: public class MyContainer { public string ContainerName { get; set; } public IList<Square> MySquares { get; set; } public IList<Circle> MyCircles { get; set; } public MyContainer() { MySquares = new List<Square>(); MyCircles = new List<Circle>(); } } public class Shape { public int Area { get; set; } } public class Square : Shape { } public class Circle : Shape { } And now I have a function like so: private static void Collect(MyContainer container) { var properties =

List<T> Casting algorithms and performance considerations

时光怂恿深爱的人放手 提交于 2019-12-11 06:35:47
问题 I have the following code class Program { static void Main(string[] args) { List<A> aList = new List<A>(); var aObj = new A(); aObj.Go(aList.Cast<IB>()); } } class A : IB { public void Go(IEnumerable<IB> interfaceList) { foreach (IB ibby in interfaceList) { Console.WriteLine("Here"); } } } interface IB { void Go(IEnumerable<IB> interfaceList); } } I originally tried passing a List but that doesn't work. After a lot of help from SO I found that passing IEnumerable is the only way to get the

Converting exponential notation numbers to strings - explanation

风流意气都作罢 提交于 2019-12-11 06:35:17
问题 I have DataFrame from this question: temp=u"""Total,Price,test_num 0,71.7,2.04256e+14 1,39.5,2.04254e+14 2,82.2,2.04188e+14 3,42.9,2.04171e+14""" df = pd.read_csv(pd.compat.StringIO(temp)) print (df) Total Price test_num 0 0 71.7 2.042560e+14 1 1 39.5 2.042540e+14 2 2 82.2 2.041880e+14 3 3 42.9 2.041710e+14 If convert float s to string s get trailing 0 : print (df['test_num'].astype('str')) 0 204256000000000.0 1 204254000000000.0 2 204188000000000.0 3 204171000000000.0 Name: test_num, dtype: