On 1/13/2015 8:31 AM, Drew Lawson wrote:
> Discussing a bit of code with a coworker, there was talk about doing
> a constructor of this form:
>
> foo::foo(Params params)
> {
> if ( !SetupFromParamsWorks(params) )
> {
> *this = foo();
> }
> }
>
> There are several obvious bad aspects to this, but the question
> that I couldn't answer was whether
> *this = foo();
> is even legal in a constructor.
>
> It feels like it should not be, but I cannot come up with a good
> reason why.
I see no reason why it would not be legal. While the object referred to
by (*this) is not fully constructed until the c-tor ends, calling the
assignment operator is legal, it's just a function. Constructing
another object (a temporary) is also legal. Unless some trickery is
attempted in the assignment operator, which is not indicated here, the
code is OK, methinks.
V
--
I do not respond to top-posted replies, please don't ask