I had a question about using unique-ptrs before. I get this answer recommending to use move-only objects. I defined a class as below:
class B {
const string objName;
public:
B ( B && ) = default;
B & operator= ( B && ) = default;
B ( const B & ) = delete;
B & operator= ( const B & ) = delete;
B(const string & name) :
objName(name) {
}
virtual ~B();
const string name() const { return objName;};
}
and I called B by this lines:
class A {
A(){}
void take(B b);
}
A a;
B b("test");
cout<<b.name();
a.take(std::move(b));
cout<<b.name();
My questions:
- Even if I have defaulted the move constructor, I can not write a.take(b) and I am getting compiling error. I understand that copy constrctor is deleted but It seems that the logical choice is to use move constructor when it is defaulted without need for writing std::move like this a.take(b)
- In the result the "test" is printed twice. Why the b object has not been removed after calling move? If the b object still exists and a copy of it has been sent to the a.take(move(b)) then it means we don't have any use of move for rvalue objects.
- Is it a good practice to use move-only objects as above (Removing copy constructor and assignment operator and defaulting move constructor and move assignment)?