casting

What should be used to check identity in C++?

I have two pointers to objects and I want to test if they are the exact same object in the most robust manner. I explicitly do not want to invoke any operator == overloads and I want it to work no matter what base classes, virtual base classes and multiple inheritance is used. My current code is this: ((void*)a) == ((void*)b) And for...

Closest C# equivalent to the F# match expression?

I'm in the situation where a lot of my classes are containers of well-known but unordered objects of different types, e.g. a container may look as follows: public class Container { public A A { get; private set; } public B B { get; private set; } public C C { get; private set; } public bool StoreIfKnown(object o) { ...

Using generics causes unchecked conversion warning

I have the following code String innerText = null; innerText = this.getException(detail.getChildElements()); causing this warning Type safety: The expression of type Iterator needs unchecked conversion to conform to Iterator The referenced method is private String getException(Iterator<OMElementImpl> iterator) { ... } The...

Why am I getting InvalidCastException when casting object to integer?

I had an odd problem, here's the setup: ASP.NET 3.5 app / MSSQLSERVER 2008 back-end. I called ExecuteScalar from my code, which returned an object, then I tried casting that object to int (i actually have a generic method T ConvertDBValue<T>(object o) {...} , but that's not as important). ExecuteScalar is hitting the following spr...

Interesting String casting

How do I make the following string cast: "ln(5*x)" -> "Math.log(5*x)" ...

Casting to one class and calling function from sibling class?

I'm getting a pointer to a base class (which is actually a pointer to some derived class). Then I want to call a function on that derived class, but I don't know which one it is. class Base { }; class DerivedOne : public Base { public: void functionA() { int x = 0; } }; class DerivedTwo : public Base { public: ...

Casting year ToInt16 not working

Hello, I have a function that basically does: internal static short GetPresentYear() { return Convert.ToInt16(DateTime.Now.Year - 1); } Now when I try using the function, the debugger shows that I'm getting 0x07d9 instead of the year 2008... What am I doing wrong here? How do I make it return 2008? Thanks. Edit: I know I can u...

When to use run-time type information?

If I have various subclasses of something, and an algorithm which operates on instances of those subclasses, and if the behaviour of the algorithm varies slightly depending on what particular subclass an instance is, then the most usual object-oriented way to do this is using virtual methods. For example if the subclasses are DOM nodes,...

Casting a 'BigStruct' to a 'SmallStruct' in C (similar structs with static arrays of different sizes)

Supposed that for some reason you are only allowed to use static memory in a C program. I have a basic structure that I am using in several places defined as below: #define SMALL_STUFF_MAX_SIZE 64 typedef struct { /* Various fields would go here */ ... double data[SMALL_STUFF_MAX_SIZE]; /* array to hold some data */ } Small...

Casting a boolean array in Java?

Is there anything I can cast a boolean array to in Java? It would be nice if I could say boolean[] bools = new boolean[8]; int j = (int)bools; But I'm not sure if that's feasible in Java. ...

C++: Casting for user defined types

How can I get the same handeling of casting for user-defined types as built in, eg: float a = 5.4; std::string s = a;//error, no conversion avaible int x = a;//warning, possible data loss int y = (int)a;//fine int z = static_cast<int>a;//fine float b = c;//warning, possible data loss Now say I have my own Int and Float class, how do I...

How can I extend a lexical cast to support enumerated types?

I have the following function that will convert a string into a numeric data type: template <typename T> bool ConvertString(const std::string& theString, T& theResult) { std::istringstream iss(theString); return !(iss >> theResult).fail(); } This does not work for enumerated types, however, so I have done something like this: ...

How to upcast an object of type IEnumerable<Foo> to IEnumerable<IFoo> if passed to the function as object?

I'm trying to upcast a parameter passed as an object (which is an upcast of an object of type IEnumerable) to IEnumerable where Foo implements IFoo. Here is an example of what I'd like to do but it does not work. public void F(object o) { //I know the object o is of type IEnumerable<Foo> where Foo implements IFoo IEnumerable<IF...

C++ How to convert string to char*

I need to convert a string to a char * for use in strtok_s and have been unable to figure it out. c_str() converts to a const char *, which is incompatible. Also, if someone could explain to me why the second strtok_s function (inside the loop) is necessary, it'd be a great help. Why do i need to explicitly advance the token rather tha...

Is using const_cast for read-only access to a const object allowed?

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result...

Cast a variable to a type represented by another Type variable?

I'm aware that questions like this have been asked before and I doubt it's possible, but I just wanted to make 100% sure that it isn't. In VB.net or C# (either language, it doesn't matter), I want to cast a variable to a type represented by another Type variable. Here's an example of the kind of code that would be needed in C#: Object...

How to cast an int's address to char pointer in C?

Hi all, Currently the below code gives me a warning when i try to compile it: int z; char *w; w = How can i cast &z properly so that w stores the pointer to z's address? ...

Action<object, EventArgs> could not be casted to EventHandler?

I was wiring up an event to use a lambda which needed to remove itself after triggering. I couldn't do it by inlining the lambda to the += event (no accessable variable to use to remove the event) so i set up an Action<object, EventArgs> variable and moved the lambda there. The main error was that it could not convert an Action<object, E...

Loading external SWF, cannot cast document class to shared base class

I have a parent SWF file that defines a Widget base class. I then load an external SWF into the parent. The external SWF's document class derives from the Widget base class -- let's call it DerivedWidget for example. The problem is that when I load the external SWF, I cannot cast the Loader.content (shows in debugger as having the Der...

AS3, loading in a SWF as a custom type

Fundamental question here. Typically in AS3 you load in a SWF via the Loader, and what you get is some sort of pseudo MovieClip that is of type "Loader". Is there any holy way under the sun to cast this loaded SWF to a custom type that extends MovieClip and not Loader, assuming the SWF was published with a base class of the custom type?...