When I studied const reference type in C++, I learned returning reference type make use local variable in main function, so I tested how returned normal struct type work in lvalue. so I expected this source won't compile, but it compiled well... :( so I have two question
"Is returned struct variable?, why this code compiled well?"
"if returned struct is not variable, why this code compiled?, I assumed that returned value is rvalue.."
#include<iostream>
#include<cstring>
using namespace std;
struct travel_time
{
int hour;
int min;
};
travel_time sumTime(travel_time, travel_time);
int main(void)
{
travel_time p1, p2,sum;
cin >> p1.hour >> p1.min;
cin >> p2.hour >> p2.min;
sum=sumTime(p1, p2);
cout << sum.hour << "hour " << sum.min<<"min";
sumTime(p1, p2) = p1; //********** why it works? **********
return 0;
}
travel_time sumTime(travel_time t1, travel_time t2)
{
travel_time sum;
sum.hour = t1.hour + t2.hour+(t1.min+t2.min)/60;
sum.min = (t1.min + t2.min) % 60;
return sum;
}