System :: Boolean和bool在C++/CLI中无法比较?

xxx*_*xxx 4 boolean c++-cli

我正在使用Visual C++ 2008 SP1 Pro.以下代码片段将无法编译:

int main(void) {
    System::Boolean^ foobar = true;
    if (foobar == true) {
        System::Console::Write("yeah!");
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下错误:

1>.\main.cpp(3) : warning C4805: '==' : unsafe mix of type 'System::Boolean ^' and type 'bool' in operation
1>.\main.cpp(3) : error C2446: '==' : no conversion from 'int' to 'System::Boolean ^'
1>        No user-defined-conversion operator available, or
1>        No standard conversion exists from the boxed form of the arithmetic type to the target type
1>.\main.cpp(3) : error C2040: '==' : 'System::Boolean ^' differs in levels of indirection from 'int'
Run Code Online (Sandbox Code Playgroud)

以下代码编译正常:

int main(void) {
    System::Boolean^ foobar = true;
    if (foobar->Equals(true)) {
        System::Console::Write("yeah!");
    }
}
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?有没有更好的方法在C++/CLI中比较System :: Boolean和bool而不是使用 - > Equals()和 - > CompareTo()?

Ale*_* C. 6

System::Boolean^是对(盒装)布尔值的引用.请System::Boolean改用.