Pro/con: Initializing a variable in a conditional statement

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

    0 讨论(0)
  • 2021-01-01 19:13

    One reason I don't normally do that is because of the common bug from a missed '=' in a conditional test. I use lint with the error/warnings set to catch those. It will then yell about all assignments inside conditionals.

    0 讨论(0)
  • 2021-01-01 19:15

    The important thing is that a declaration in C++ is not an expression.

    bool a = (CThing* pThing = GetThing()); // not legit!!
    

    You can't do both a declaration and boolean logic in an if statement, C++ language spec specifically allows either an expression or a declaration.

    if(A *a = new A)
    {
        // this is legit and a is scoped here
    }
    

    How can we know whether a is defined between one term and another in an expression?

    if((A *a = new A) && a->test())
    {
        // was a really declared before a->test?
    }
    

    Bite the bullet and use an internal if. The scope rules are useful and your logic is explicit:

    if (CThing* pThing = GetThing())
    {
        if(pThing->IsReallySomeThing())
        {
        }
    }
    
    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2021-01-01 19:19

    So many things. First of all, bare pointers. Please avoid them by all means. Use references, optional, unique_ptr, shared_ptr. As the last resort, write your own class that deals with pointer ownership and nothing else.

    Use uniform initialization if you can require C++11 (C++14 preferred to avoid C++11 defects): - it avoids = vs == confusion and it's stricter at checking the arguments if there are any.

    if (CThing thing {})
    {
    }
    

    Make sure to implement operator bool to get predictable conversion from CThing to bool. However, keep in mind that other people reading the code would not see operator bool right away. Explicit method calls are generally more readable and reassuring. If you can require C++17, use initializer syntax.

    if (CThing thing {}; thing.is_good())
    {
    }
    

    If C++17 is not an option, use a declaration above if as others have suggested.

    {
      CThing thing {};
      if (thing.is_good())
      {
      }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题