a special kind of operator
-
operator float() { return m_fNum; }
I am working on a port from C++ to C# of an AI method. But I found this method and don't know how to port this one to C#. Anyone does? Greetings.... :)Hi Willem, It looks like an overloaded cast operator. You'd probably want to consider using a 'Single' data type first of all in C#, but when it comes to the code using "(float)MyClass" on your particular class, you'd probably want to replace that with a method call or property that returns the single-point floating result. Regards, Joe
-
Hi Willem, It looks like an overloaded cast operator. You'd probably want to consider using a 'Single' data type first of all in C#, but when it comes to the code using "(float)MyClass" on your particular class, you'd probably want to replace that with a method call or property that returns the single-point floating result. Regards, Joe
MultiThread wrote: but when it comes to the code using "(float)MyClass" on your particular class, you'd probably want to replace that with a method call or property that returns the single-point floating result. Why wouldn't he just keep it as an overloaded cast operator?
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek
-
MultiThread wrote: but when it comes to the code using "(float)MyClass" on your particular class, you'd probably want to replace that with a method call or property that returns the single-point floating result. Why wouldn't he just keep it as an overloaded cast operator?
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek
Hi David, He probably could, but that would be due to my ignorance. I did some research and found that the cast operator in C# can't be overloaded, but that C# did allow the use of "explicit" and "implicit" as keywords to allow for that sort of behavior. Regards, Joe
-
Hi David, He probably could, but that would be due to my ignorance. I did some research and found that the cast operator in C# can't be overloaded, but that C# did allow the use of "explicit" and "implicit" as keywords to allow for that sort of behavior. Regards, Joe
-
I found the solution :)
public static explicit operator float(Fuzzy f1) { return f1.fuzzNum; }
You can then do this:float x; Fuzzy y = new Fuzzy(56.34); x = (float)y; //x is now 56.34
Greetings.... :)Cool...glad we could help. :) Although, you may want to use operator overloading sparingly. Only use it when it makes sense to have an object cast to another object...etc.
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek