I'm trying to build a timer in C++.
In my Timer class there are two Date objects that hold a std::unique_ptr<struct tm>
pointer.
After I std::move
the second unique_ptr
in the second Date object in the following piece of code, it points to the same memory address of the first one, effectively making the two objects represent the same time, even though they should be different because of the duration offset.
using namespace std;
time_t localTime = time(nullptr);
unique_ptr<struct tm> currentTime = static_cast<unique_ptr<struct tm>>(localtime(&localTime));
startDate = Date(std::move(currentTime));
time_t endTime = time(nullptr) + duration;
unique_ptr<struct tm> timerEndTime = static_cast<unique_ptr<struct tm>>(localtime(&endTime));
endDate = Date(std::move(timerEndTime));
The Date
constructor being called is this:
Date::Date(std::unique_ptr<struct tm> time) : date(std::move(time)) {}
What am I doing wrong?