I would like to implement a "take" method. A "take" method is something like a get method, but which steals the getted object from its owner: the owner is left with that object in an empty state. Of course, this involves the C++11 move semantics. But which of the following is the best way to implement it?
class B
{
public:
A take_a_1()
{
return std::move(m_a);
}
// this can be compiled but it looks weird to me that std::move
// does not complain that I'm passing a constant object
A take_a_2() const
{
return std::move(m_a);
}
A&& take_a_3()
{
return std::move(m_a);
}
// this can't be compiled (which is perfectly fine)
//
// A&& take_a_4() const
// {
// return std::move(m_a);
// }
private:
A m_a;
};