警告C4800:'BOOL':强制将值设置为bool'true'或'false'(性能警告) [英] warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
问题描述
当我在Visual Studio 2008中编译以下代码段代码时,会收到此警告.
When I compile the below code snippet code in Visual studio 2008, I get this warning.
BOOL
CPlan::getStandardPlan() const
{
return m_standardPlan;
}
bool m_bStandardPlan;
if(plan!=NULL)
{
// Assign the values to the Cola object
poCola->m_lPlanId = plan->getPlanId();
poCola->m_lPlanElementId = plan->getPlanElementId();
poCola->m_lPlanElementBaseId = plan->getPlanElementBaseId();
poCola->m_bStandardPlan = plan->getStandardPlan(); //C4800
return 1;
}
我引用了以下链接,
http://msdn.microsoft. com/en-us/library/b6801kcy%28v = vs.90%29.aspx
警告C4800:'int ':将值强制设置为布尔值"true"或"false"(性能警告)
我不确定如何解决此警告.
I'm not sure how to fix this warnings.
推荐答案
BOOL
是WinAPI中int
的typedef. WinAPI是C API,因此它们不能使用C ++的bool
.如果您无法通过从函数返回bool
来摆脱它,例如因为您不维护该功能,所以可以对零使用显式检查以消除警告:
BOOL
is a typedef for int
somewhere in WinAPI. WinAPI is a C API, so they can't use C++'s bool
. If you can't get rid of it by returning a bool
from the function, e.g. because you don't maintain the function, then you can use an explicit check against zero to get rid of the warning:
poCola->m_bStandardPlan = (plan->getStandardPlan() != 0);
另一个考虑因素是添加一个封装支票的函数:
Another consideration would be to add a function that encapsulates the check:
bool getStandardPlan(CPlan const& plan) {
return plan->getStandardPlan() != 0;
}
然后
poCola->m_bStandardPlan = getStandardPlan(plan);
这篇关于警告C4800:'BOOL':强制将值设置为bool'true'或'false'(性能警告)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!