#define TAMSEC 59
#define TAMMIN 59
#define TAMHOURS 23
using namespace std;
class Time{
private:
int hours,
minutes,
seconds;
public:
Time(int hours = 0, int minutes = 0, int seconds = 0);
/**Sobre carga operador <<**/
friend ostream& operator << (ostream& out, const Time &obj);
/**Sobrecarga operador (Pre incremento ++)**/
Time& operator ++();
/**Sobrecarga operador (Pos incremento ++)**/
Time operator ++(int);
};
/**Sobrecarga operador (Pre incremento ++)**/
Time& Time::operator ++(){
seconds ++;
if(seconds > TAMSEC){
minutes ++;
seconds = 0;
if(minutes > TAMMIN){
minutes = 0;
hours ++;
if(hours > TAMHOURS)
hours = 0;
}
}
return *this;
}
/**Sobrecarga operador (Pos incremento ++)**/
Time Time::operator++(int){
Time aux = Time(*this);
seconds ++;
if(seconds > TAMSEC){
minutes ++;
seconds = 0;
if(minutes > TAMMIN){
minutes = 0;
hours ++;
if(hours > TAMHOURS)
hours = 0;
}
}
return aux;
}
I was developing the overload of operators of the post and pre-increment of my Time class, my doubt is when I do the pos increment, at the beginning I wanted to return the reference of my object (* aux) that I created within the member function before increase its attributes, but it does not leave me, so I had to return the object itself. Why can not I return the reference of my aux object?
Time& Time::operator++(int){
Time aux = Time(*this);
seconds ++;
if(seconds > TAMSEC){
minutes ++;
seconds = 0;
if(minutes > TAMMIN){
minutes = 0;
hours ++;
if(hours > TAMHOURS)
hours = 0;
}
}
return *aux;
}