Menu

operator++(?)

Anonymous
2002-10-16
2012-09-26
  • Anonymous

    Anonymous - 2002-10-16

    class counter
    { int amount;
      public:
      counter() { amount=0; }
      counter operator++(?) //why does it have to be " counter operator++(int) "
                           //not " counter operator++(void) "
         { amount++; return *this; }
    };

    main()
    {
    counter a;
    a++;
    }

     
    • Anonymous

      Anonymous - 2002-10-17

      so maybe anyone could tell me how to use ++ operator with friend and without friend function:
      1) ++(something) using friend function
      2) (something)++ using friend
      3) ++(something) not using friend function
      4) (something)++ -"-

       
    • Anonymous

      Anonymous - 2002-10-17

      i guess if i want to use ++(something) operator, i have to use friend function, and (something)++ can t be defined using friend function.

      my program:

      #include <stdlib.h>
      #include <iostream.h>

      class counter
      { int amount;
        public:
        void show() { cout << amount << "\n\n"; }
        counter() { amount=0; }
        counter operator++(int) { amount++; return *this; }
        friend counter operator++(counter& x) { x.amount++; return x; }
      };

      main()
      {
      counter object;
      object++;
      ++object;
      object.show();
      system("pause");
      }

      am i right??

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.