Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overwrite inside constructor

49 views
Skip to first unread message

Drew Lawson

unread,
Jan 13, 2015, 8:31:36 AM1/13/15
to
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.

--
Drew Lawson | Though it's just a memory,
| some memories last forever

Victor Bazarov

unread,
Jan 13, 2015, 9:05:10 AM1/13/15
to
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

Paavo Helde

unread,
Jan 13, 2015, 12:28:48 PM1/13/15
to
dr...@furrfu.invalid (Drew Lawson) wrote in news:m936n7$8ca$1
@raid.furrfu.com:

> 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.

This is kosher, assuming SetupFromParamsWorks() leaves the object in a
state which the assignment operator can cope with. If the object remains
in some half-constructed state (uninitialized pointers, etc), but the
assignment operator assumes a valid state (as it normally does), then of
course problems may arise.

Cheers
Paavo

Drew Lawson

unread,
Jan 13, 2015, 1:16:06 PM1/13/15
to
In article <XnsA421C6216E621m...@216.196.109.131>
Paavo Helde <myfir...@osa.pri.ee> writes:

>This is kosher, assuming SetupFromParamsWorks() leaves the object in a
>state which the assignment operator can cope with.

Thanks all. It struck me that I rarely deal with temps in a
constructor. (I go for a factory type method when that is a need.)
So I never got a feel for any extra restrictions that might apply.


--
Drew Lawson While they all shake hands
and draw their lines in the sand
and forget about the mess they've made
0 new messages