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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
The difference is that the prefix form takes an additional parameter, and int that is not used and is passed as 0

continues.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.