Pro/con: Initializing a variable in a conditional statement

前端 未结 12 1328
Happy的楠姐
Happy的楠姐 2021-01-01 18:24

In C++ you can initialize a variable in an if statement, like so:

if (CThing* pThing = GetThing())
{
}

Why would one consider this bad or g

12条回答
  •  忘掉有多难
    2021-01-01 19:16

    Just an FYI some of the older Microsoft C++ compliers(Visual Studios 6, and .NET 2003 I think) don't quite follow the scoping rule in some instances.

    for(int i = 0; i > 20; i++) {
         // some code
    }
    
    cout << i << endl;
    

    I should be out of scope, but that was/is valid code. I believe it was played off as a feature, but in my opinion it's just non compliance. Not adhering to the standards is bad. Just as a web developer about IE and Firefox.

    Can someone with VS check and see if that's still valid?

提交回复
热议问题