I have a function that takes two member variables of the current class and sets them to a timeval structure and returns the value of timeval obj (by value).
I see a problem when setting the timeval element of a class level object against creating a new timeval object every time get () is called.
Inside class
protected:
int time[2];
timeval tv;
timeval getTimeval()
{
tv.tv_sec = (time_t)time[0];
tv.tv_usec = time[1];
return tv;
}
This will not return the correct time values. Tv.tv_sec will be overwritten, but tv_usec will remain constant. However, it will return the correct values ββwhen I create the timeval object inside the get call.
timeval getTimeval()
{
timeval t;
t.tv_sec = (time_t)time[0];
t.tv_usec = time[1];
return t;
}
Is there any reason why setting timeval objects in a member variable should be different than creating a new object and setting its values?
source
share