Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 561
Dekakaruk Avatar asked Dec 09 '25 08:12

Dekakaruk


1 Answers

Create a helper function to construct the parent where you can add sequencing:

parent make_parent(::other_class &&oth) {
    auto sf = some_function(oth.some_property);
    return parent(sf, std::move(oth));
}

some_class(::other_class oth) :
    parent(make_parent(std::move(oth))
{}
like image 172
1201ProgramAlarm Avatar answered Dec 10 '25 20:12

1201ProgramAlarm