Hint of operator overloading

Discussion in 'C++' started by dover, Jul 23, 2004.

  1. dover

    dover Guest

    Could somebody give hints about writing overloading of post-increment and
    pre-increment operators? Thanks!
     
    dover, Jul 23, 2004
    #1
    1. Advertising

  2. dover

    Nolan Martin Guest

    Nolan Martin, Jul 23, 2004
    #2
    1. Advertising

  3. Here, try this. The trick is the dummy parameter (int) in the pre-increment
    operator, which is used to disambiguate the ++foo and foo++ operators.

    dave

    class foo
    {

    public:
    foo( int i )
    :_i(i)
    {}
    foo()
    :_i(0)
    {}
    // pre-increment ++foo
    foo operator++()
    {
    _i++;
    return *this;
    }
    // post increment foo++
    foo operator++(int )
    {
    foo tmp = *this;
    _i++;
    return tmp;

    }
    int toInt()
    {
    return _i;
    }
    private:
    int _i;

    };



    #include "stdafx.h"

    int main(int argc, char* argv[])
    {

    foo f(10);
    foo g(10);
    foo gnew(++g);
    foo fnew(f++);

    int gi = g.toInt();
    int fi = f.toInt();

    int gnewi = gnew.toInt();
    int fnewi = fnew.toInt();


    return 0;
    }


    "dover" <> wrote in message
    news:p82Mc.125754$...
    > Could somebody give hints about writing overloading of post-increment and
    > pre-increment operators? Thanks!
    >
    >
    >
     
    Dave Townsend, Jul 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    440
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    757
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    746
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    577
  5. Replies:
    2
    Views:
    328
Loading...

Share This Page