-1

I am trying to use the increment operator in a derived class. But I am not able to achieve this.

#include<iostream>
using namespace std;

class Base {
    private:
        int a;
    public:
        int x, y, z;
        int Converter(int data) {
            cout << "Printing z: " << z << endl;
            return data * x + y;
        }
        Base& operator=(int rhs) {
            a = Converter(rhs);
            return *this;
        }
        Base(int a, int x, int y, int z) : a(a), x(x), y(y), z(z) {}
        explicit Base(int a_) : a(a_) {}
        Base operator+(const Base& obj){ 
            Base x(*this);
            return Base(x.a + obj.a);
        }
        Base& operator++() { return *this = *this + Base(Converter(1)); }
};

class Derived : Base {
    public:
        Derived() : Base(0, 1, 2, 3) {} // Come constants for x, y, z
        explicit Derived(int data) : Base(data, 1, 2, 3){}
        using Base::operator=;
        using Base::operator++;
};

int main(){
    
    Derived x(10);
    ++x; 
    x = 1;
}
   

I am thinking the problem is with the highlighted line that was commented above. When I tried to print the values x, y, z in the operator++() function it is showing 0 eventhough I have initialized them as 1, 2, 3 in the Derived class definition.

I tried to call the other constructor as below but still no use.

Base& operator++() { return *this = *this + Base(Converter(1), this->x, this->y, this->z) }

The output produced is:

Printing z: 3
Printing z: 0

If I invert the order in main() like below, the output is different.

x = 1;
++x; 

Output:

Printing z: 3
Printing z: 3

Can someone point me in the right direction?

Thanks.

RD1153
  • 115
  • 8
  • This code shouldn't compile. You're promising to return a `Base` in `operator+()`, but you return nothing. – scohe001 Dec 24 '20 at 14:43
  • @scohe001 I have repasted after correcting the mistakes. Now it compiles. – RD1153 Dec 24 '20 at 14:48
  • This program exhibits undefined behavior, by way of reaching the closing brace of the non-`void` function `Base::operator+` without encountering a `return` statement. – Igor Tandetnik Dec 24 '20 at 14:49
  • @RD1153 It does not compile *without warnings*. [Why should I always enable compiler warnings?](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – JaMiT Dec 24 '20 at 14:49
  • *"When I tried to print the values x, y, z in the operator++() function"* -- you should keep this attempt in your [mre] so we can reproduce your results. – JaMiT Dec 24 '20 at 14:51
  • 2
    There are several errors, and they're pretty basic stuff. The compiler should be helping you out here. Read the errors and warnings. – Kenny Ostrom Dec 24 '20 at 14:51
  • I tried to create a MWE of my actual problem. I have a cpp file where I have defined the operator+ definition. – RD1153 Dec 24 '20 at 15:01

1 Answers1

1

The meaning of the code is impossible to work out. I have no idea what you are trying to do.

But the reason for the Printing z: 0 output is clear enough. It comes from this constructor

explicit Base(int a_) : a(a_) {}

(which actually leaves z uninitialised but I guess you get 0 on your system)

That constructor is being called here

return Base(x.a + obj.a);

and that return statement is being invoked from the overloaded operator+ here

Base& operator++() { return *this = *this + Base(Converter(1)); }

That sequence overwrites the value of *this with the object that has z equal to 0, and that value is output on the next line of code x = 1;.

As I say, I have no idea what this code is trying to do so I have no idea what to suggest as a fix, but hopefully this explains where that value is coming from.

john
  • 85,011
  • 4
  • 57
  • 81