C++ 如何重置高分辨率时钟::时间点

C++ 如何重置高分辨率时钟::时间点,c++,c++11,chrono,C++,C++11,Chrono,我正在开发一个类Timer,它的一些成员属于time\u point定义为typedef chrono::time\u point time\u point的类型 问题: 此对象的默认值是什么 我需要从以下两个原因了解此值: 知道成员是否已初始化 实现Timer::Reset()函数 背景 那么,我是否可以通过只查看成员m_tpStart来实现Timer::WasStarted?我不想为此添加布尔成员 那么,我可以通过只查看成员m_tpStart来实现Timer::wastarted吗 如果你定

我正在开发一个类
Timer
,它的一些成员属于
time\u point
定义为
typedef chrono::time\u point time\u point的类型

问题: 此对象的默认值是什么

我需要从以下两个原因了解此值:

  • 知道成员是否已初始化
  • 实现
    Timer::Reset()
    函数
  • 背景 那么,我是否可以通过只查看成员
    m_tpStart
    来实现
    Timer::WasStarted
    ?我不想为此添加布尔成员

    那么,我可以通过只查看成员m_tpStart来实现Timer::wastarted吗

    如果你定义了这样的不变量,
    m_tpStart
    为零(历元),当且仅当计时器被重置(未启动)时,那么它是微不足道的。只需检查start是否已启动,即可测试计时器是否已启动

    确切地说,如何将时间点设置为epoch,似乎有点复杂——我想这就是你所说的“如何重置高分辨率时钟::时间点”。您需要复制并指定一个默认构造的时间点

    void Start() { m_tpStart = high_resolution_clock::now(); }
    void Stop() {
        m_tpStop = high_resolution_clock::now();
    }
    bool WasStarted() {
        return m_tpStart.time_since_epoch().count(); // unit doesn't matter
    }
    void Reset() {
        m_tpStart = m_tpStop = {};
    }
    

    您读过吗?可能重复您想要的
    定时器::Reset()
    做什么?重置计时器意味着什么?我刚刚更新了问题。请看这门课本身。希望它能解决问题。@user207933,我理解
    Timer::Reset()
    函数需要更多细节。因此,为了简单起见,让我们关注函数
    Timer::WasStarted
    的实现。它能以简单的方式实现吗?您将如何使用它实现
    GetDuration()
    。@sp2danny我更改了代码,在停止计时器时不重置start。这更适合OP的设计
    GetDuration
    现在很简单。减法并施法。
    void Start() { m_tpStart = high_resolution_clock::now(); }
    void Stop() {
        m_tpStop = high_resolution_clock::now();
    }
    bool WasStarted() {
        return m_tpStart.time_since_epoch().count(); // unit doesn't matter
    }
    void Reset() {
        m_tpStart = m_tpStop = {};
    }