Overloading C ++ operators, is the auxiliary variable necessary in this case?

2

I'm studying through the book Introduction to Object Oriented Programming with C ++ (Antonio Mendes da Silva Filho), and I implemented an example of it as follows:

#include <iostream>

using namespace std;

class Contador{

    public:
        Contador(int c = 0){contador = c;};
        int getContador(){return contador;};
        Contador operator++(){
            ++contador;
            return contador; };
    private:
        unsigned int contador;
};

int main(){

    Contador c1, c2, c3;

    ++c1;
    ++c2;
    ++c2;
    ++c2;
    c3 = ++c2;

    cout << "c1: " << c1.getContador() << endl;
    cout << "c2: " << c2.getContador() << endl; 
    cout << "c3: " << c3.getContador() << endl; 

    return 0;
}

The return of the counter is only for the assignment to be made in c3 = ++c2;

The program ran as expected, printing

c1 = 1
c2 = 4
c3 = 4

But in the response of the book he adds an auxiliary variable in the following sentence:

Contador operator++(){
            ++contador;
            Contador temp;
            temp.contador = contador;
            return temp; };

Compiling, the result is exactly the same. This auxiliary variable is unnecessary, right?

    
asked by anonymous 03.01.2017 / 16:17

2 answers

3

First the code does not work properly not. See that depending on how the operator is used gives trouble . The book example is a bit better because at least it returns an object of type Contador and not the variable contador , which are very different things. But it does not work either . This works:

#include <iostream>
using namespace std;

class Contador{
    unsigned int contador;
public:
    Contador(int c = 0) { contador = c; };
    int getContador() { return contador; };
    Contador& operator++() {
        ++contador;
        return *this;
    };
};

int main() {
    Contador c1, c2, c3;
    ++c1;
    ++c2;
    ++c2;
    ++c2;
    c3 = ++c2;
    ++(++(++c1));
    cout << "c1: " << c1.getContador() << endl;
    cout << "c2: " << c2.getContador() << endl; 
    cout << "c3: " << c3.getContador() << endl; 
}

See running on ideone .

So it is ok for the pre increment operator because now you have a reference to the object being returned and not using a temporary object that the compiler creates.

Will you do the post increment as well? There you need a temporary object to return as a result of the operation and do the operation on the object. So:

#include <iostream>
using namespace std;

class Contador{
    unsigned int contador;
public:
    Contador(int c = 0) { contador = c; };
    int getContador() { return contador; };
    Contador& operator++() {
        ++contador;
        return *this;
    };
    Contador operator++(int) {
        Contador temp(*this);
        ++*this;
        return temp;
    };
};

int main() {
    Contador c1, c2, c3;
    ++c1;
    ++c2;
    ++c2;
    ++c2;
    c3 = ++c2;
    ++(++(++c1));
    c1++;
    c2++;
    c3++;
    cout << "c1: " << c1.getContador() << endl;
    cout << "c2: " << c2.getContador() << endl; 
    cout << "c3: " << c3.getContador() << endl; 
}

See working on ideone .

If you have not made any confusion with the two operators, perhaps the book is not so good, but I can not say without knowing it and knowing the context in which it is used.

    
03.01.2017 / 17:39
1

I suppose he has put the temp variable to leave in a more didactic way, since he would be returning 2 times the counter and thus could generate doubts.

    
03.01.2017 / 16:36