Pro/con: Initializing a variable in a conditional statement

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

    if (CThing* pThing = GetThing())
    

    It is bad style, because inside the if you are not providing a boolean expression. You are providing a CThing*.

    CThing* pThing = GetThing();
    if (pThing != NULL)
    

    This is good style.

提交回复
热议问题