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

<pre><code>if (CThing* pThing = GetThing())
{
}
</code></pre>

<p>Why would one consider this bad or good style? What are the benefits and disadvantages?</p>

<p>Personally i like this style because it limits the scope of the pThing variable, so it can never be used accidentally when it is NULL. However, i don't like that you can't do this:</p>

<pre><code>if (CThing* pThing = GetThing() &amp;&amp; pThing-&gt;IsReallySomeThing())
{
}
</code></pre>

<p>If there's a way to make the above work, please post. But if that's just not possible, i'd still like to know why.</p>

<p><a href="https://stackoverflow.com/questions/136373/initializing-a-variable-in-a-conditional-statment">Question borrowed from here, similar topic but PHP.</a></p>

This
should
doesn't work in C++
since
even though it supports
short circuiting evaluation
.
Maybe
Don't try the following:
```
```
if ((CThing* pThing = GetThing()) && (pThing->IsReallySomeThing()))
{
}
```
```
err.. see
Wesley Tarle's answer