0

I'm reading about move semantic in C++11 and now I'm trying to understand the implementation of the move constructor. Suppose that we have the following class:

struct A {
    A(){ }
    virtual ~A(){ }
    A(const A&&){ }
};

struct B{
    int i;
    A a;
    virtual ~B(){ }
    B(const B&& b){
        i = b.i;
        i = 0;
        //How to move a??
    }
};

My question is how to invoke the A's move constructor inside B's one body? I'd use std::swap, but looking for it I found some description. The arguments are of the lvalue reference type, so it has nothing to do with move semantic. What to do?

stella
  • 2,546
  • 2
  • 18
  • 33

1 Answers1

5
B(B&& b)
  : i(b.i)
  , a(std::move(b.a))
{
}

Note the argument is no longer const (it never should have been), and all initialization can be done using the initialization list for optimal efficiency.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • But in that case we don't set source values to `nullptr` or something else... I mean to perform the moving, not copying. I'm a bit confused by [this](http://stackoverflow.com/a/3109981/4671213) answer, where he sets the source to `nullptr `explicitly. – stella Dec 15 '15 at 18:58
  • @stella: You don't need to do that. – John Zwinck Dec 16 '15 at 01:39