Friday, November 28, 2008

C++ if statement oddity

I found that you can do work such as assigning a variable and executing a method outside of the main expression of an if statement. For example:



    if(hRes = GetApplicationState(), NT_SUCCESS(hRes))
{
// Do something
}



Yes, that is a comma inside of the if statement. To the left of the comma is executed first and then the if statement uses the right side as its evaluation expression.

No comments: