Pro/con: Initializing a variable in a conditional statement

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

    This shoulddoesn't work in C++ sinceeven though it supports short circuiting evaluation. MaybeDon't try the following:

    if ((CThing* pThing = GetThing()) && (pThing->IsReallySomeThing()))
    {
    }
    

    err.. see Wesley Tarle's answer

提交回复
热议问题