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

Asked

Viewed 143 times

2

I am studying for the book Introduction to Object-Oriented Programming with C++ (Antonio Mendes da Silva Filho), and 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 counter return is only so that the assignment can be made on c3 = ++c2;

The program ran as expected, printing

c1 = 1
c2 = 4
c3 = 4

But in the answer of the book he adds an auxiliary variable in the following passage:

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

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

2 answers

3


First the code does not work properly no. See that depending on how the operator is used gives problem. The book example is a little better because at least it returns an object of the type Contador and not the variable contador, that are very different things. But it doesn’t work either. That’s how it 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; 
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This is ok for the preincrement operator because you now have a reference to the object being returned and not using a temporary object that the compiler creates.

Will you do the post increment too? Then 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; 
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

  • I kept reading and later he switched this part for Contador operator++(){ &#xA; ++contador;&#xA; return Contador(contador);&#xA;}

  • Which is probably a mistake too.

  • that Contador(contador) is calling the builder?

  • Exactly, creates a new instance, but at the very least is not efficient.

  • About post-increment, I just implemented it like this: Contador operator++(int){&#xA; return contador;&#xA; contador++;&#xA; }

  • Its does not increase anything, because the increment occurs after the return, never gets on that line. His is increasing before. He’s not making a mess not?

  • Oops, I did, actually his is return Contador(contador++);, I copied wrong.

Show 2 more comments

1

I suppose he put the temp variable to leave in a more didactic way, because he would be returning twice the counter and so could generate doubts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.