-2

** Pre Increment **

#include <iostream>
using namespace std;
class demo {
    int i;
    public:
    demo (int b = 0) {
        i = b;
    }
    void display() {
        cout<<"Number is " <<i<<endl;

    }
    void operator ++() {
        i = ++i;
    }
};

int main() {
    demo obj(5);
    obj.display();
    ++obj;
    obj.display();
    return 0;
}

In this case operator function took no function argument

** Post Increment **

#include <iostream>
using namespace std;
class demo{
    int i;
    public:
        demo (int b = 0 ){
            i = b;
        }
        void display(){
            cout<<"Number is " <<i<<endl;

        }
        void operator++(int){
            i = i++;
        }
};

int main(){
    demo obj(5);
    obj.display();
    obj++;
    obj.display();
    return 0;
}

While in this code the function took the int as and argument

In these codes almost everything is same except for the fact that in one code void operator ++() takes a argument int while in other it doesn't .

What could be the possible reason of such behaviour ?

  • 1
    thats not the only difference. One callls `i = ++i;` the other `i = i++;`, both should actually be just `++i` – 463035818_is_not_an_ai Jun 30 '22 at 08:36
  • 1
    Thats the syntax for overload the pre cs post increment operator, otherwise the functiom declarations would be the same. – gerum Jun 30 '22 at 08:37
  • 2
    "What could be the possible reason of such behaviour ?" what behavior? You didnt say what is "strange" – 463035818_is_not_an_ai Jun 30 '22 at 08:37
  • Depending on C++ standard used by the compiler, `i = i++` might be *undefined behavior*. Later versions of the standard made it sequenced IIRC. – Some programmer dude Jun 30 '22 at 08:38
  • I don't understand the question. What do you mean by "strange behaviour"? It **seems as though** you are asking "why does `operator++` have a parameter when it is used to implement post-increment, but not when it is used to implement pre-increment?". The answer is: because *that is how the compiler knows which thing you are implementing*. – Karl Knechtel Jun 30 '22 at 08:46
  • 1
    If it's about the argument to Your `operator++` function, please take some time to think about how the compiler would know which function to call if both post- and pre-increment both used the same function signature. And remember that you can overload *both* operators in the same class. – Some programmer dude Jun 30 '22 at 08:49
  • 1
    The pre/post increment operators are supposed to return a value, a different one. As is you only have an "increment" operator that is neither pre nor post and also UB in older C++ versions. – Goswin von Brederlow Jun 30 '22 at 09:13

1 Answers1

1

If you are asking why one takes a parameter and one does not. I could only say, that is just the syntactical convention in C++ to distinguish prefix increment and postfix increment.

In your above code, the resultant object is always incremented after you use the operator ++. Therefore, you would not likely to see a desirable result. You can only notice their difference it you assign demo x = a++; or demo x = ++a; (getting their current value), and then print the value out.

To do that, you have to do something like below:

Prefix increment increases, and return itself.

demo& operator ++() {
    i = i + 1;
    return *this; // Return itself after incremented
}

Meanwhile, postfix increment creates a duplicate object, increases itself, and return the duplicate one.

demo operator++() {
    demo duplicate(i);
    i = i + 1;
    return duplicate; // Return the itself but before incremented.
}

You can read this familiar post: Overloading ++ for both pre and post increment

Khoa LT
  • 89
  • 1
  • 6