Valhalla Legends Archive

Programming => General Programming => Topic started by: Fr0z3N on July 12, 2004, 12:06 PM

Title: C++ to C# something is backwards?
Post by: Fr0z3N on July 12, 2004, 12:06 PM
it's like:

(Variable + 6) = Array[5];

how the hell would that convert to C#? It's like backwards C++.

Variable = (Array[5] + 6); would make more sence.
Title: Re:C++ to C# something is backwards?
Post by: Eibro on July 12, 2004, 01:31 PM
Quote from: Rubicon on July 12, 2004, 12:06 PM
it's like:

(Variable + 6) = Array[5];

how the hell would that convert to C#? It's like backwards C++.

Variable = (Array[5] + 6); would make more sence.
No, those two aren't equivalent. The only way I see the first being a valid statement is if it were *(Variable + 6) = Array[5];, since (Variable + 6) isn't an l-value and thus cannot be assigned to.
Title: Re:C++ to C# something is backwards?
Post by: Yoni on July 12, 2004, 04:58 PM
Surely you can see the statement being valid. Just overload operator+ to return a reference...
Title: Re:C++ to C# something is backwards?
Post by: MyndFyre on July 12, 2004, 06:00 PM
Quote from: Yoni on July 12, 2004, 04:58 PM
Surely you can see the statement being valid. Just overload operator+ to return a reference...

First, you can't overload operators like that in C#....

Second, Eibro was correct in saying that *(Variable + 6) would be correct syntax.  You'd need to put that within an unsafe code block and also compile with the /unsafe switch.
Title: Re:C++ to C# something is backwards?
Post by: K on July 12, 2004, 06:07 PM
Quote from: Myndfyre on July 12, 2004, 06:00 PM
Quote from: Yoni on July 12, 2004, 04:58 PM
Surely you can see the statement being valid. Just overload operator+ to return a reference...

First, you can't overload operators like that in C#....

Second, Eibro was correct in saying that *(Variable + 6) would be correct syntax.  You'd need to put that within an unsafe code block and also compile with the /unsafe switch.

Yoni is talking about Eibro's comment that the original code:

(Variable + 6) = Array[5];


could not compile in C++ since (Variable + 6) would not be an L-Value.  He is correct in saying that since we are not given a type for Variable, its possible that the +operator is overloaded and (Variable+6) is returning a reference, and hence a valid L-value.

To convert the code

*(Variable + 6) = Array[5];


to C# probably wouldn't require using unsafe code if we were given more information, such as the type of Variable.  It would simply require changing the pointer arithmatic to an array index.
Title: Re:C++ to C# something is backwards?
Post by: Eibro on July 12, 2004, 06:41 PM
Yes, I thought of that too, but returning a reference from opertor+() doesn't make sense either.