Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. a special kind of operator

a special kind of operator

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
6 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WillemM
    wrote on last edited by
    #1

    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.... :)

    M 1 Reply Last reply
    0
    • W WillemM

      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.... :)

      M Offline
      M Offline
      MultiThread
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • M MultiThread

        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

        D Offline
        D Offline
        David Stone
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • D David Stone

          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

          M Offline
          M Offline
          MultiThread
          wrote on last edited by
          #4

          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

          W 1 Reply Last reply
          0
          • M MultiThread

            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

            W Offline
            W Offline
            WillemM
            wrote on last edited by
            #5

            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.... :)

            D 1 Reply Last reply
            0
            • W WillemM

              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.... :)

              D Offline
              D Offline
              David Stone
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups