Operator ++

From Yefu's notes
Jump to: navigation, search

The grammar of operator ++ is quite different in C# and C++. In C#, it should be a static member of the class:

public class LogItem
    public static LogItem operator ++(LogItem item) 
       item.visitTimes ++;
       return item;

Also, in C#, a++ and ++a uses the same operator.

In C++, it is required that the post-increment version of operator ++ uses a dummy parameter with the type of int.

class MyIter
    // Definition of pre-increment:
    //    ++object;
    MyIter& operator++()
        /* Increment the iterator as appropriate
           You should be changing the object in place

        // Once you are done return yourself.
        return *this;
    // Definition of post-increment:
    //    object++;
    MyIter operator++(int)
        // Post increment (returns the same value) so build the result.
        MyIter  result(*this);

        // Now do the increment using pre-increment on the current object

        // return the result.
        return result;