-1

I was playing with overloading different operators and added print statements to watch what was happening. When I overloaded the post increment operator, I saw that the constructor was being called twice, but I don't understand why.

#include <iostream>
using namespace std;

class ParentClass {
    public:
    ParentClass() {
        cout << "In ParentClass!" << endl;
    }
};

class ChildClass : public ParentClass {
    public:
        int value;
        ChildClass() { }
        ChildClass(int a)
        : value(a) {  
            cout << "In ChildClass!" << endl;
        }

        int getValue() { return value; } 

        ChildClass operator++( int ) {
            cout << "DEBUG 30\n";
            this->value++;
            return this->value; 
        }
};

int main() {
    cout << "DEBUG 10\n";
    ChildClass child(0);
    cout << "value initial     = " << child.getValue() << endl;
    cout << "DEBUG 20\n";
    child++;
    cout << "DEBUG 40\n";
    cout << "value incremented = " << child.getValue() << endl;
}

The output after running this code is:

DEBUG 10
In ParentClass!
In ChildClass!
value initial     = 0
DEBUG 20
DEBUG 30
In ParentClass!
In ChildClass!
DEBUG 40
value incremented = 1
yamex5
  • 195
  • 1
  • 11
  • Note that the code overloads the **post**-increment operator but implements a **pre**-increment. – Pete Becker Nov 25 '17 at 13:04
  • @PeteBecker Maybe I'm missing something. I thought that adding the parameter 'int' in operator++(int) implements post incrementation? – yamex5 Nov 25 '17 at 19:55
  • 1
    The code returns the incremented value. That’s what pre-increment does. Post-increment should return the original value. – Pete Becker Nov 26 '17 at 02:03
  • @PeteBecker You are correct sir! I was thinking that the incremented value had to be returned, but now I realize that it only has to be incremented to simulate the behavior of integers. – yamex5 Nov 26 '17 at 06:46

1 Answers1

1

This statement

  return this->value; 

Says return int

But the method prototype is

 ChildClass operator++( int ) 

So the compiler thinks, got an int need a ChildClass - Lets construct one from the int. Hence the output

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • I was following an example for binary operators where a new type with the result is returned. I removed the return statement, and indeed the constructor is called but once now. And also changing the return type to int works. Thanks! – yamex5 Nov 25 '17 at 06:47
  • 1
    @yamex5 , having `ChildClass`'s `++` return `int` is a really nasty surprise for anyone expecting the usual behaviour of `operator++`. See [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) for an extensive write-up on operator overloading. – user4581301 Nov 25 '17 at 07:26
  • @user4581301 - It is not returning an `int`. See the prototype and my explaination – Ed Heal Nov 25 '17 at 07:27
  • 1
    I know that. I was warning yamex that the course of action they outlined at the end of their comment was a bad idea. – user4581301 Nov 25 '17 at 07:29