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?