casting

Does casting actually DO anything?

此生再无相见时 提交于 2021-02-20 17:18:09
问题 Consider the following snippet: char x[100]; double *p = &x; As expected, this yields this warning: f.c:3:15: warning: initialization of ‘double *’ from incompatible pointer type ‘char (*)[100]’ [-Wincompatible-pointer-types] 3 | double *p = &x; | ^ This is very easy to solve by just changing to double *p = (double*)&x; My question here is, does the casting actually DO anything? Would the code be invalid without the cast? Or is it just a way to make the compiler quiet? When is casting

How to move a Vec<Box<dyn Trait>> Into Vec<Rc<RefCell<dyn Trait>>>

浪子不回头ぞ 提交于 2021-02-20 10:39:12
问题 I have a Vec<Box<dyn Trait>> as input, and I want to store its elements in a Vec<Rc<RefCell<dyn Trait>>> . What is the best way to do it? I tried with: use std::cell::RefCell; use std::rc::Rc; trait Trait {} fn main() { let mut source: Vec<Box<dyn Trait>> = Vec::new(); let mut dest: Vec<Rc<RefCell<dyn Trait>>> = Vec::new(); for s in source { let d = Rc::new(RefCell::new(s.as_ref())); dest.push(d); } } But I got the error: error[E0277]: the trait bound `&dyn Trait: Trait` is not satisfied -->

why does explicit cast for generic list not work

拥有回忆 提交于 2021-02-20 00:48:38
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

why does explicit cast for generic list not work

巧了我就是萌 提交于 2021-02-20 00:48:00
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

why does explicit cast for generic list not work

半世苍凉 提交于 2021-02-20 00:44:33
问题 I am trying to cast a list of objects within a consturctor for a derive class IntersectionPath as follows. public class IntersectionPath : Path<IntersectionSegment>, IEnumerable { //Constructors public IntersectionPath() : base() { Verts = null; } public IntersectionPath(List<Intersection> inVerts, List<Segment<Node>> inEdges) : base() { this.Segments = (List<IntersectionSegment>) inEdges; } } Segments is defined in the generic base class Path public class Path<T> : IEnumerable<T> where T :

Bind to arbitrary Dictionary<,> by using a converter to cast object

烈酒焚心 提交于 2021-02-19 04:15:27
问题 I'm trying to workaround the difficulties in binding to a Dictionary in WinRT Xaml (also referenced here). I want to use a converter to do this rather than having to change all of my view-models or business code to return a List of a custom Key Value class. This means I need to cast an object to a List<> of some type. public object Convert(object value, Type targetType, object parameter, string temp) { if(value is IDictionary) { dynamic v = value; foreach (dynamic kvp in v) { } } return /

Bind to arbitrary Dictionary<,> by using a converter to cast object

只愿长相守 提交于 2021-02-19 04:07:15
问题 I'm trying to workaround the difficulties in binding to a Dictionary in WinRT Xaml (also referenced here). I want to use a converter to do this rather than having to change all of my view-models or business code to return a List of a custom Key Value class. This means I need to cast an object to a List<> of some type. public object Convert(object value, Type targetType, object parameter, string temp) { if(value is IDictionary) { dynamic v = value; foreach (dynamic kvp in v) { } } return /

Bind to arbitrary Dictionary<,> by using a converter to cast object

两盒软妹~` 提交于 2021-02-19 04:06:00
问题 I'm trying to workaround the difficulties in binding to a Dictionary in WinRT Xaml (also referenced here). I want to use a converter to do this rather than having to change all of my view-models or business code to return a List of a custom Key Value class. This means I need to cast an object to a List<> of some type. public object Convert(object value, Type targetType, object parameter, string temp) { if(value is IDictionary) { dynamic v = value; foreach (dynamic kvp in v) { } } return /

Casting from member pointer to whole struct/class

a 夏天 提交于 2021-02-19 03:50:26
问题 Consider following code: #include <iostream> struct bar { double a = 1.0; int b = 2; float c = 3.0; }; void callbackFunction(int* i) { auto myStruct = reinterpret_cast<bar*>(i) - offsetof(bar, b); std::cout << myStruct->a << std::endl; std::cout << myStruct->b << std::endl; std::cout << myStruct->c << std::endl; //do stuff } int main() { bar foo; callbackFunction(&foo.b); return 0; } I have to define a callback function and I want to use some additional information in that function. I defined

How to write Implicit Conversion from an Interface to another type?

与世无争的帅哥 提交于 2021-02-19 02:36:05
问题 I am trying to do something like below: public class SomeWrapper : ISomeWrapper{ public static implicit operator ActualRec(ISomeWrapper someWrapper) { return ((SomeWrapper)someWrapper).SomeInfo; } } But this code fails, saying: "Either parameter or return type must be of type SomeWrapper". I Understand the problem that compiling is stating. But I need this type conversionb because throughout my application I am using ISomeWrapper as the variable, storing SomeWrapper instance. (Also,