简体   繁体   中英

How do I cast a bool to a BOOL?

Am I safe in casting a C++ bool to a Windows API BOOL via this construct

bool mybool = true;
BOOL apiboolean = mybool ? TRUE : FALSE;

I'd assume this is a yes because I don't see any obvious problems but I wanted to take a moment to ask only because this may be more subtle than it appears.

Thanks to Dima for (gently) pointing out my carelessness in the way I'd originally phrased the question.

Do you mean


bool b;
...
BOOL apiboolean = b ? TRUE : FALSE;

If so, then yes, this will work.

Yes, that will work, but

bool b;
...
BOOL apiboolean = (BOOL) b;

should work just as well, as does the reverse:

bool bb = (bool) apiboolean;

Visual Studio 2005 will simply accept:

bool b = true;
BOOL apiboolean = b;

no casting required .

Note that the other way round BOOL->bool does not simply work like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM