how to immigrate c++'s union?
-
I just wanna transfer a piece of code from cpp code to csharp. To my surprisse, csharp doesn't have the union key word. Then how can cope with the union in cpp to keep the code change minimized. any suggestion?
C# is a type safe language, and a union is every thing that a typesafe language is not. Therefore you will have to create a conversion operator or function to cast from type A to type B.
-
I just wanna transfer a piece of code from cpp code to csharp. To my surprisse, csharp doesn't have the union key word. Then how can cope with the union in cpp to keep the code change minimized. any suggestion?
If you need to interop with a C/C++ object that uses a union you can do that, but there isn't a way to create a union in C# that behaves the same way (well easily create a union, you can re-create the VT_VARIANT type by using an enum and a lot of casting). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
-
If you need to interop with a C/C++ object that uses a union you can do that, but there isn't a way to create a union in C# that behaves the same way (well easily create a union, you can re-create the VT_VARIANT type by using an enum and a lot of casting). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
Well, I try to use set and get to simulate the union. It seems work in my eye. class A { public int a; } class B { public char b; } class union { object obj; public A aa { get { return (A)obj; } set { obj=value; } } public B bb { get { return (B)obj; } set { obj=value; } } }