Search code examples
c++constructorc++17moveorder-of-execution

How to use variable and move variable at the same time?


Let's assume we have the following code:

struct some_class : parent
{

    some_class(::other_class oth) :
       parent(some_function(oth.some_property), std::move(oth))
    {}

};

Of course construction results in undefinied behaviour (crash in my case), since c++ does not specify execution order. But then, how can I retreive the property before the movement? I cannot change the parent.


Solution

  • You could try a delegating constructor:

    struct some_class : parent
    {
    
        some_class(::other_class oth) :
           some_class(some_function(oth.some_property), std::move(oth))
        {}
    
    private:
        some_class(const ::Foo& foo, ::other_class&& oth) :
           parent(foo, std::move(oth))
        {}
    };