c++ - Move constructors in inheritance hierarchy -
i have doubt related move constructors in inheritance hierarchy. in c++ primer (stanley lippman) mentioned move constructor in inheritance hierarchy defined this:
class base { /* default copy control , move constructor */ }; class d : public base { public: d(const d& d) : base(d) {/*initializers members of d */} //this ok d(d&& d): base(std::move(d)) {/*initializers members of d */} };
in move constructor, tried removing std::move while calling base move constructor since 'd' rvalue reference.
d(d&& d): base(d) {/*initializers members of d */}
but ended calling base class copy constructor instead of move constructor.
to understand why std::move required, searched forum see previous discussions , found out few replies said though 'd' rvalue reference, within derived class's move constructor still lvalue. hence need call std::move on ensure base class move constructor called. understood part.
but c++ primer, understand once std::move called should not use object @ after that. after expression in std::move called ends, object remain in valid state destruction values holds may not meaningful.
so when call std::move delegate base class's move constructor how object remain in meaningful state when come body of derived class's move constructor.
in other words:
d(d&& d): base(std::move(d)) { // 'd' in meaningful state here? // after std::move(d), can still use 'd' here? }
i understand base class move members related base class alone , derived class members won't touched. exception after std::move base part of object in valid destructed state , derived part still in meaningful state. please me understand this.
class base { // data members... public: base(base&& other) = default; }; class derived { // data members... public: derived(derived&& other) : base(std::move(other)) { ... } };
the derived
move constructor casts other
rvalue using std::move
, passes result base
move constructor, involves implicit cast derived&&
base&&
.
the base
move constructor might steal guts of base class members of other
, can't touch guts of derived class members because sees base&&
.
Comments
Post a Comment