Partial re-initialization of C++ object through constructor
<p>I am looking for an optimal pattern for partial re-initialization of a C++ object. </p>

<p>With partial re-initialization I mean that some members (<code>step_param</code> in the code example) need to keep its values and other members (<code>value</code> in the code example) are re-init'ed.  </p>

<p><strong>Important point:</strong> The bloat and redundancy of an init() or reset() member function that does basically the same as the constructor should be avoided.</p>

<p>So far I have the following solution:</p>

<pre><code>namespace reinit_example
{
    struct reinit_t {} reinit;

    struct stepper_t
    {
        int step_param; // keep parameter
        int value;

        stepper_t()
            : step_param(1)
            , value(step_param)
        {}
        stepper_t( const stepper_t &amp; c, reinit_t )
            :   step_param(c.step_param)
            ,   value(step_param)
        {}

        void step()
        {
            value += step_param;
        }

    };

    void use_cases_1()
    {
        stepper_t c;
        // use c
        c.step();
        // and later reinit
        c = stepper_t(c,reinit);
    }
} // namespace
</code></pre>

<p>It should also work well with inheritance and composition:</p>

<pre><code>namespace reinit_example
{
    struct stepper_2_t : public stepper_t
    {
        int step_param_2; // keep parameter
        int value_2;

    public:
        stepper_2_t()
            : step_param_2(0)
            , value_2(step_param_2)
        {}

        stepper_2_t( const stepper_2_t &amp; cc, reinit_t )
            :   stepper_t(cc)
            ,   step_param_2(cc.step_param_2)
            ,   value_2(step_param+2)
        {}
        void step()
        {
            stepper_t::step();
            value_2 += value + step_param_2;
        }
    };

    struct stepper_comp_t
    {
        stepper_t c;
        stepper_2_t cc;
    public:
        stepper_comp_t()
        {}
        stepper_comp_t( const stepper_comp_t &amp; d, reinit_t )
            :   c(d.c,reinit)
            ,   cc(d.cc,reinit)
        {}
        void step()
        {
            c.step();
            cc.step();
        }
    };

    void use_cases_2()
    {
        stepper_2_t cc;
        // use cc, change config
        cc.step();
        // maybe change config
        cc.step_param = 2;
        // reinit
        cc = stepper_2_t(cc,reinit);

        stepper_comp_t d;
        d = stepper_comp_t(d,reinit);
    }
} // namespace
</code></pre>

<p>C++11 non-static member initialization makes it even simpler:</p>

<pre><code>#if __has_feature(cxx_nonstatic_member_init)
namespace reinit_example
{
    struct stepper_11_t
    {
        int step_param = 0 ; // keep value
        int value = step_param;

        stepper_11_t()
        {}

        stepper_11_t( const stepper_11_t &amp; c11, reinit_t )
            :   step_param(c11.step_param)
        {}
    };

    void use_cases_3()
    {
        stepper_11_t c11;
        c11 = stepper_11_t(c11,reinit);
    }

} // namespace
#endif
</code></pre>

<p>For testing:</p>

<pre><code>int main()
{
    reinit_example::use_cases_1();
    reinit_example::use_cases_2();
#if  __has_feature(cxx_nonstatic_member_init)
    reinit_example::use_cases_3();
#endif
}
</code></pre>

<p>Solution proposed by Jerry Coffin: Parameters are moved into a separate struct, which is  passed to the constructor to reinit.</p>

<pre><code>namespace reinit_example
{

    struct stepper_config_t
    {
        struct config_t
        {
            config_t()
                :   step_param(1)
            {}
            int step_param;
            int other_param;
        };
        config_t config;
        int value;

        stepper_config_t()
            : value(config.step_param)
        {}

        stepper_config_t( const config_t &amp; c)
            :   config(c)
            ,   value(c.step_param)
        {}

        void step()
        {
            value += config.step_param;
        }

    };

    void use_cases_4()
    {
        stepper_config_t c;
        // use c
        // and later reinit
        c = stepper_config_t(c.config);
    }
} // namespace
</code></pre>
