casting

using integer arrays on mex

两盒软妹~` 提交于 2019-12-12 17:15:43
问题 I want to pass an integer array from MATLAB to C. I've tried using the type uint64_T but it keeps crashing. Getting a double pointer using mxGetPr() works fine, but I need to typecast the content into an integer and doing so in every iteration might be slow. I've tried this in the mexFunction: uint64_T *l; l= (uint64_T *)mxGetData(prhs[1]); The function calling this variable is of this form: void XAction( const double *v, const uint64_T *l, double *w) { for (j=c; j; j--) for (i=r-1; i; i--) w

Casting List<MyObject> to IEnumerable<MyInterface>

血红的双手。 提交于 2019-12-12 16:57:11
问题 I'm still learning some of this c# stuff, and I couldn't find an answer to this question. Assuming that I have a list of MyObject implementing MyInterface public class MyObject : IMyInterface { ...} public List<MyObject> MyObjectList; How can I return an IEnumerable<IMyInterface> with the contents of MyObjectList? I mean, right now I have this: List<IMyInterface> temp = new List<IMyInterface>(); foreach (MyObject obj in MyObjects) temp.Add(obj); return (IEnumerable<IMyInterface>)temp; But is

Is it unsafe to mix static and reinterpret cast when casting to and back from void*?

[亡魂溺海] 提交于 2019-12-12 16:27:11
问题 Simply: If i static_cast a type X* to void* , is it always safe to reinterpret_cast it back to X*? I am unable to produce any case where this fails for example: #include <iostream> struct a { int* m_array; }; int main() { bool fail = false; for(int i = 0; ++i < 5000;) { a* pA = new a; pA->m_array = new int [i+1]; // A new size of data everytime pA->m_array[i] = 10; void* pvA = static_cast<void*>(pA); pA = reinterpret_cast<a*>(pvA); if(pA->m_array[i] != 10) { fail = true; break; } delete []pA-

Casting float to string without scientific notation

99封情书 提交于 2019-12-12 16:23:54
问题 The float: fl = 0.000005 casts to String as str(fl)=='5e-06' . however, I want it to cast as str(fl)='0.000005' for exporting to CSV purposes. How do I achieve this? 回答1: Use fl = 0.00005 s = str('%8.5f' % fl) print s, type(s) Gives 0.00005 <type 'str'> In case you want no extra digits, use %g fl = 0.0005 s = str('%g' % fl) print s, type(s) fl = 0.005 s = str('%g' % fl) print s, type(s) Gives 0.0005 <type 'str'> 0.005 <type 'str'> 回答2: You can just use the standard string formatting option

Casting Type array to Generic array?

隐身守侯 提交于 2019-12-12 16:13:23
问题 The short version of the question - why can't I do this? I'm restricted to .NET 3.5. T[] genericArray; // Obviously T should be float! genericArray = new T[3]{ 1.0f, 2.0f, 0.0f }; // Can't do this either, why the hell not genericArray = new float[3]{ 1.0f, 2.0f, 0.0f }; Longer version - I'm working with the Unity engine here, although that's not important. What is - I'm trying to throw conversion between its fixed Vector2 (2 floats) and Vector3 (3 floats) and my generic Vector<> class. I can

Cast one derrived class to another without changing base class

左心房为你撑大大i 提交于 2019-12-12 15:53:18
问题 I have several child classes all having same parent. Each child class can be constructed using some data that is contained in parent object. I would like to cast one child to be another child using info contained in base object (without modifying base object). Currently it is implemented as illustrated in following example: #include <iostream> using namespace std; class Data {}; class base { public: base() {} base(Data input) : data(input) {} virtual ~base() { cout << "Deleting :" << this-

How can I cast an Interface as its type in c#?

对着背影说爱祢 提交于 2019-12-12 15:18:05
问题 I have a property that returns an interface. During debugging I can break on what was returned and while it is the interface, Visual Studio is smart enough to know the derived type that it actually is. I assume it's using reflection or something. I'm not sure. My question is, can I have that same info available to me at runtime so I can create a variable of the appropriate type and cast the interface as that? Here is what I am saying: IPreDisplay preDisplay = cb.PreDisplay; If preDisplay is a

Java Reference assignment with generic lists

夙愿已清 提交于 2019-12-12 15:00:09
问题 I feel stupid asking this but I am. The line List<HasId> ids = list is giving a compile error in the following code: public class MyGarbageClass { public void myMethod(List<MyCompany> list){ List<HasId> ids = list; } interface HasId { int getId(); } class MyCompany implements HasId{ private int id = 5; @Override public int getId() { return id; } } } MyCompany implements HasId so I thought I should be able to assign it. Why cant I? And more importantly , what is an easy way to assign this to

How to cast an object to the class returned by getClass()

此生再无相见时 提交于 2019-12-12 13:17:13
问题 ProtostuffIOUtil.mergeFrom(data,o,RuntimeSchema.getSchema(o.getClass())); How to cast Object to getClass() Class. With the above method call, I will get a compile error because the method requires that the o variable is of the same class as the o.getClass() parameter. How does one get around this? 回答1: Class clazz = o.getClass(); ProtostuffIOUtil.mergeFrom(data, clazz.cast(o), RuntimeSchema.getSchema(clazz)); 回答2: Cast an object to the class from getClass() using the cast() method: myObj

Why do I have to cast an enum element when assigning it to a same enum variable type in C?

这一生的挚爱 提交于 2019-12-12 13:15:00
问题 I have the following: typedef enum { FLS_PROG_SUCCESS, FLS_PROG_FAIL, FLS_ERASE_SUCCESS2U, FLS_ERASE_FAIL, FLS_READ_SUCCESS, FLS_READ_FAIL, FLS_FORMAT_SUCCESS, FLS_FORMAT_FAIL }FLS_JobResult_t; void Foo(void) { FLS_JobResult_t ProgramStatus; /* Then I try to initialize the variable value */ ProgramStatus = FLS_PROG_SUCCESS; ... } Innocent uh, but when compiling MISRA C gives the error: The value of an expression shall not be assigned to an object with a narrower essential type or of a