首页 > 解决方案 > 在 Visual Studio 2019 中将布尔变量作为参数传递给需要 BOOL 参数的函数

问题描述

拿这个示例代码:

void CChristianLifeMinistryDiscussionsDlg::SetControlStates()
{
    if (m_pEntry == nullptr)
        return;

    bool bClass1 = false, bClass2 = false;
    m_pEntry->GetAuxiliaryClasses(bClass1, bClass2);

    m_cbBrothersC1.EnableWindow(bClass1 ? TRUE : FALSE);
    m_cbBrothersC2.EnableWindow(bClass2 ? TRUE : FALSE);

}

EnableWindow需要一个类型的参数BOOL。我永远不清楚在这种情况下只传递我的bool变量的值是否可以接受。

标签: c++visual-c++mfcboolean

解决方案


ABOOL是 的类型别名int。type 的值bool可以隐式转换为 type 的值int。这称为积分提升,并且定义明确:值false变为 0,值true变为 1。

将类型值传递给需要类型bool参数BOOL(ie int) 的函数是安全且定义明确的。


推荐阅读