Move constructors and inheritance

前端 未结 2 573
温柔的废话
温柔的废话 2021-01-01 17:54

I am trying to understand the way move constructors and assignment ops work in C++11 but I\'m having problems with delegating to parent classes.

The code:

         


        
2条回答
  •  滥情空心
    2021-01-01 18:35

    One of the more confusing things about functions taking rvalue references as parameters is that internally they treat their parameters as lvalues. This is to prevent you from moving the parameter before you mean to, but it takes some getting used to. In order to actually move the parameter, you have to call std::move (or std::forward) on it. So you need to define your move constructor as:

    T(T&& o): T0(std::move(o)) { puts("move"); }
    

    and your move assignment operator as:

    T& operator=(T&& o) { puts("move assign"); return static_cast(T0::operator=(std::move(o))); }
    

提交回复
热议问题