Differentiation between pre and post increment operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohammad ismail aakhil
    New Member
    • Aug 2007
    • 1

    Differentiation between pre and post increment operator

    i have overladed unary ++ both pre and postfix increment operator using member function. both the operator function differ by arguements.v use a additional argunent of type int.
    how does complier differentiate between pre and post increment operator.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by mohammad ismail aakhil
    i have overladed unary ++ both pre and postfix increment operator using member function. both the operator function differ by arguements.v use a additional argunent of type int.
    how does complier differentiate between pre and post increment operator.
    When the compiler see this: "++foo" it generates code for the (overloaded)
    pre-increment operator and when the compiler see this: "foo++" it generates
    code for the (overloaded) post-increment operator. T* operator++() is the prefix
    increment operator and T* operator++(int) is the postfix increment operator
    for your type T.

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Adding to what JosAH has already said, the int argument is a hack by the C++ language architects. Without it, the prefix and postfix member functions would have the same function prototype. So an int was arbitrarily added to make them unique.

      If you try to use the int argument, your compiler will produce an error that says operator++ does not take any arguments.

      Comment

      Working...