I’m using Visual C++ 2008 SP1 Pro. Following code snippet will fail to compile:
int main(void) {
System::Boolean^ foobar = true;
if (foobar == true) {
System::Console::Write("yeah!");
}
}
It gives following errors:
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'
Following code compiles fine:
int main(void) {
System::Boolean^ foobar = true;
if (foobar->Equals(true)) {
System::Console::Write("yeah!");
}
}
Am I doing something wrong? Is there better way to compare System::Boolean with bool in C++/CLI than using ->Equals() and ->CompareTo()?
System::Boolean^is a reference to a (boxed) boolean. UseSystem::Booleaninstead.