overloading the ++ operator

how do i overload the prefix ++ operator and the postfix ++ operator respectively?
are there any differences in the syntax?
shai78Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
The difference is that the prefix form takes an additional parameter, and int that is not used and is passed as 0

continues.
0
 
nietodCommented:
Nope, had that backwards, It is the POSTFIX form that takes the additional parameter.

Thus

class Cls
{
public:
   Cls &operator ++ (); // Prefix (++x).
   Cls &operator ++(int); // Postfix (x++).
};

Note that the int is not specified by the programmer when the postfix ++ is called, it is supplied by the C++ compiler and is always passed as a 0.

Let me know if you have any questions.
0
 
KangaRooCommented:
Mh, postfix would normally not return a reference but a temporary. It can be implemented using the prefix version:

Cls operator ++(int); // Postfix (x++)
{
  Cls tmp(*this);
  ++(*this);// or operator++();
  return tmp;
}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nietodCommented:
True, i was focusing the necessary difference in syntax and missed that.   Although that diffference is needed only if you want the operators to have the "standard" behavior", the int parameter is always needed regardless of the behavior of the operators.
0
 
LucHoltkampCommented:
The () is not needed, ++*this will do fine.
0
 
KangaRooCommented:
May not be required (dereference operator has a very high priority) for the compiler, but I have my needs. Well, we've had a style 'discussion' recently...
0
 
nietodCommented:
Not needed in this case, but what about in *SomePointer++?
0
 
LucHoltkampCommented:
In fact Kangaroo, the priority is the same for prefix++ and *, but because associativity is right to left, * comes first.
In nietod's case: postfix++ has a HIGHER priority then * so ++ comes first then....
Yes, styles... It's always difficult to decide to bracket an expression or not...
I'm somewhere in between, I always have a precedence list hanging on my PC, so.... I used to write 'turbo' code in my C days (try to write the entire program in one line :-) but I no longer do that....
Luc
0
 
KangaRooCommented:
Indeed,
  1.4  Expressions                                           [gram.expr]
          primary-expression:
                  literal
                  this
                  :: identifier
                  :: operator-function-id
                  :: qualified-id
                  ( expression )
                  id-expression

          id-expression:
                  unqualified-id
                  qualified-id
          id-expression:
                  unqualified-id
                  qualified-id

          unqualified-id:
                  identifier
                  operator-function-id
                  conversion-function-id
                  ~ class-name
                  template-id
          qualified-id:
                  nested-name-specifier templateopt unqualified-id
          nested-name-specifier:
                  class-or-namespace-name :: nested-name-specifieropt

          class-or-namespace-name:
                  class-name
                  namespace-name
          postfix-expression:
                  primary-expression
                  postfix-expression [ expression ]
                  postfix-expression ( expression-listopt )
                  simple-type-specifier ( expression-listopt )
                  postfix-expression . templateopt ::opt id-expression
                  postfix-expression -> templateopt ::opt id-expression
                  postfix-expression . pseudo-destructor-name
                  postfix-expression -> pseudo-destructor-name
                  postfix-expression ++
                  postfix-expression --
                  dynamic_cast < type-id > ( expression )
                  static_cast < type-id > ( expression )
                  reinterpret_cast < type-id > ( expression )
                  const_cast < type-id > ( expression )
                  typeid ( expression )
                  typeid ( type-id )

          expression-list:
                  assignment-expression
                  expression-list , assignment-expression
          pseudo-destructor-name:
                  ::opt nested-name-specifieropt type-name :: ~ type-name
                  ::opt nested-name-specifieropt ~ type-name
          unary-expression:
                  postfix-expression
                  ++  cast-expression
                  --  cast-expression
                  unary-operator cast-expression
                  sizeof unary-expression
                  sizeof ( type-id )
                  new-expression
                  delete-expression
          unary-operator: one of
                  *  &  +  -  !  ~
          new-expression:
                  ::opt new new-placementopt new-type-id new-initializeropt
                  ::opt new new-placementopt ( type-id ) new-initializeropt
          new-placement:
                  ( expression-list )
          new-type-id:
                  type-specifier-seq new-declaratoropt
          new-declarator:
                  ptr-operator new-declaratoropt
                  direct-new-declarator
          direct-new-declarator:
                  [ expression ]
                  direct-new-declarator [ constant-expression ]
          new-initializer:
                  ( expression-listopt )
          delete-expression:
                  ::opt delete cast-expression
                  ::opt delete [ ] cast-expression
          cast-expression:
                  unary-expression
                  ( type-id ) cast-expression

That is one reason to use parenthesis ;)
0
All Courses

From novice to tech pro — start learning today.