rifter1818 Posted July 8, 2004 Posted July 8, 2004 in C++ i could just state say #define Lerp(a,b,t)(a + ((b-a)*t)) and be on my way but this does not work quite so happily in c#, anyways i like my loosly defining simple equations (i mananged a close to 75% code reduction with perlin noise (to the point that i can now understand my code where as it took me a month to fully understand Ken Perlins code (although granted i didnt know c++ at the begining of that month). Anyways thanks in advance. Quote
*Experts* Nerseus Posted July 8, 2004 *Experts* Posted July 8, 2004 The #define in C++ did not carry over to C#. As far as I know, there is no equivalent. When using #define in C++, you're basically defining a "macro function" - it's only for inlining code. I guess they decided that wasn't a necessity with C#, the same way they left out the "inline" keyword for functions/methods. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Administrators PlausiblyDamp Posted July 8, 2004 Administrators Posted July 8, 2004 (edited) In C# they deliberately didn't allow #define macros on the grounds that they are not typesafe and can introduce subtle errors that way. Also they can have unexpected side effects for the un-wary - classic example being #define max (a,b) (((a)>(b))?(a):(b)) int i=1,j=2; int k = max(i++,j++) //what's the value of i,j,k now ? Edited July 9, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
rifter1818 Posted July 9, 2004 Author Posted July 9, 2004 In C# they deliberately didn't allow #define macros on the grounds that they are not typesafe and can introduce subtle errors that way. Also they can have unexpected side effects for the un-wary - classic example being #define max (a,b) (((a)>(b))?(a):(b)) int i=1,j=2; int k = max(i++,b++) //what's the value of i,j,k now ? well darn i liked not having to strongly type every variable... oh well its not that big of a deal, and shouldnt k = 2, i = 2,b = 3 after the abouve mentioned code as arnt the ++ operaters supposed to be acted opon after the function? example while(cin >> Array[index++]){} well fair enough thats not actually a function but you get my point. anyways thanks for your input you two :D Quote
Administrators PlausiblyDamp Posted July 9, 2004 Administrators Posted July 9, 2004 Problem is a define isn't a function but a literal text substitution, one of the arguments would have the ++ operator invoked twice the other once. #define max (a,b) (((a)>(b))?(a):(b)) k= max(i++,j++) ; //above expands to k = (((i++)>(j++))?(i++):(j++)) //spot the error? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.