why some operators cannot be overloaded?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • velvizhi
    New Member
    • Nov 2009
    • 1

    why some operators cannot be overloaded?

    why the operators like scope resolution operator,condit ional operator,size of operator cant be overloaded?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Generally the operators that can't be overloaded are like that because overloading them could and probably would cause serious program errors or it is syntactically not possible,

    For instance the sizeof operator returns the size of the object or type passed as an operand. It is evaluated by the compiler not at runtime so you can not overload it with your own runtime code. It is syntactically not possible to do. Even if it was pointer arithmetic relies on the correct value being returned by this operator since the compiler already knows how to calculate the correct value all overloading would do would be to allow you to calculate an incorrect value, something that would almost certainly lead to the program not working correctly.

    Scope resolution and member access operators work on names rather than values. C++ has no syntax for writing code that works on names rather than values so syntactically these operators can not be overridden.

    Again what useful purpose would overloading the conditional operator produce? I can think of none.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      All operators that can be overloaded must have at least one argument that is a user-defined type. That is, operator overloading can apply only to your own classes.

      That means you can't overload operator+ for int or double.

      That means you can'toverload the scope resolution operator since it has no arguments.

      That means you can't invent new operators.

      etc...

      Comment

      Working...