警告C4800:'BOOL':强制将值设置为bool'true'或'false'(性能警告) [英] warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

查看:589
本文介绍了警告C4800:'BOOL':强制将值设置为bool'true'或'false'(性能警告)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆