jeudi 6 juin 2019

Problem with pre increment and post increment operator overloading

I have seen an example with pre increment and post increment operator overloadings in c++.I have accordingly written my code but when I try to execute it nothing is happening.I mean: no error and no output. Is my way of using the operator overloading wrong?

      sint &operator++(){
      ++(*this);
      return *this;
      };    // prefix ++: no parameter, returns a reference

  sint operator++(int){
      sint copy;
      copy = *this;
        ++(*this);
      return copy;
      };  // postfix ++: dummy parameter, returns a value

  /*here I am focusing mainly on ++ operator overloading and so I havnt 
    posted the class structure*/

     int main() {
  sint a = 1;   //this is handled using assignment operator sint 
                    //contains value as private member of int type 
  sint b(41);    //overloading  and copy operator overloading
   sint c = a + b;
       c++;
       sint kk = ++c;
       cout << c << '\n';
       cout << kk << '\n';   
       }

Aucun commentaire:

Enregistrer un commentaire