Pro/con: Initializing a variable in a conditional statement

前端 未结 12 1223
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:03

    You can have initialization statements inside if and switch since C++17.

    Your code would now be:

    if (CThing* pThing = GetThing(); pThing->IsReallySomeThing())
    {
        // use pThing here
    }
    // pThing is out of scope here
    

提交回复
热议问题