Class overoaded operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scienceprof
    New Member
    • Jul 2008
    • 2

    Class overoaded operators

    I want to write Exponent class that overoads the ^ operator so it can be used to perform exponential notation. For instance if you use the statement
    int iResult = 10 ^ 3;, the iResult variable should be assigned a value of 1000. How can I write that?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Hmmm. Please refresh my memory and tell me what the ^ operator does.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by weaknessforcats
      Hmmm. Please refresh my memory and tell me what the ^ operator does.
      It bitwise exclusive or's both of its integral operands. AFAICT you can't overload an
      operator for two primitives (such as int).

      kind regards,

      Jos

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Not to mention it is very poor practice in any class to overload any arithmetic operator to a different arithmetic function. It is bound to cause confusion, mis-use and hours of debugging.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by JosAH
          Quote:
          Originally Posted by weaknessforcats
          Hmmm. Please refresh my memory and tell me what the ^ operator does.


          It bitwise exclusive or's both of its integral operands. AFAICT you can't overload an
          operator for two primitives (such as int).
          Hook, line and sinker. The OP was to answer this. Banfa has the correct take on this.

          Comment

          Working...