class CDayTime
{
public:
int AddTime(int iSeconds);
private:
void *m_pUser;
int m_iHour;
int m_iMinute;
int m_iSecond;
};bool CDayTime::IsValid()
{
if (m_iHour < 0 || m_iHour > 23) return false;
if (m_iMinute < 0 || m_iMinute > 59) return false;
if (m_iSecond < 0 || m_iSecond > 59) return false;
return true;
}assert(m_pUser != NULL);前置条件并不是对输入参数的检查,而是对内部状态的检查,对某一些必须满足的条件的检查。
assert(iRet != 0);iRet表示函数的返回值,返回值不为0,表示失败。
class CheckInvariant
{
public:
CheckInvariant(CDayTime *pTime)
: m_pTime(pTime)
{
assert(m_pTime->IsValid());
}
~CheckInvariant()
{
assert(m_pTime->IsValid());
}
private:
CDayTime *m_pTime;
}int CDayTime::AddTime(int iSeconds)
{
CCheckInvariant oCheck(this);
int iRet;
//计算
return iRet;
}
int CDayTime::AddTime(int iSeconds)
#ifdef ENABLE_DBC
{
assert(m_pUser != NULL);
CCheckInvariant oCheck(this);
int iRet = uncheck_AddTime(iSeconds);
assert(iRet != 0);
return iRet;
}
int CDayTime::uncheck_AddTime(int iSeconds)
#endif
{
int iRet;
//计算
return iRet;
}Imperfect C++读书笔记(二):前置条件、后置条件和不变式
原文:http://blog.csdn.net/play_fun_tech/article/details/19626043