void func()
{
is_finished = false);
// do something ...
is_finished = true);
}
{
is_finished = false);
// do something ...
is_finished = true);
}
since the code must be written in pairs, one at the very beginning, and the other the very end, using C++ RAII idiom is a bit better.
class bool_guard
{
public:
bool_guard(bool& b, bool init_value = true):m_bool(b)
{
m_bool = init_value;
}
~bool_guard()
{
m_bool = !m_bool;
}
private:
bool& m_bool;
};
{
public:
bool_guard(bool& b, bool init_value = true):m_bool(b)
{
m_bool = init_value;
}
~bool_guard()
{
m_bool = !m_bool;
}
private:
bool& m_bool;
};
now, we can write code like this:
void func()
{
bool_guard b(false);
// do something ...
}
{
bool_guard b(false);
// do something ...
}
No comments:
Post a Comment