I have a function which takes two current class level member variables and sets them into a timeval structure, and returns the timeval obj (by value).
I am seeing an issue when setting a class level member timeval object vs creating a new timeval object at each get() call.
Inside the class
protected:
int time[2];
timeval tv;
// work done on setting the time array
timeval getTimeval()
{
tv.tv_sec = (time_t)time[0];
tv.tv_usec = time[1];
return tv;
}
This will not return the correct timeval values. The tv.tv_sec will get overwritten but the tv_usec remains 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 setting the timeval objects on a member variable should differ from creating a new object and setting its values?