我有一个计时器类,里面有一个标志。当我们没有调用的init(),标记为0后,我们调用的init(),它将我们所说的端部(后置为1),它将被重新设置为0。如何正确设置标志
class TimerHao
{
private:
double seconds;
int flag=0; //0: Have not start accumulation. 1: In accumulation, between init() and end();
public:
void init();
void end();
};
void TimerHao::init()
{
if(flag!=0) { throw runtime_error("ERROR!!! Cannot initial the timer before it is ended!"); }
...
flag=1;
}
void TimerHao::end()
{
if(flag!=1) { throw runtime_error("ERROR!!! Cannot end the timer before it is initialized!");}
...
flag=0;
}
我可以通过使用代码:
TimerHao timerhao;
timerhao.init();
...
timerhao.end();
...
timerhao.init();
...
timerhao.end();
...
我不喜欢标志设置为一个整数,我需要阅读的注释,以了解其含义。实际上,我在我的代码中使用了这种标志,例如标志可以是0,1,2,3,5,每个数字意味着不同的事物。有时候,我对自己的代码感到困惑,我必须仔细阅读注释以了解我在做什么。有没有一种明确的方法来处理这个标志?谢谢。
如何使用'enum'来代替? –
你想要的是一个枚举 – UKMonkey
为它添加一个'enum'。 –