move-assignment-operator

Questions about the move assignment operator

时光总嘲笑我的痴心妄想 提交于 2019-12-03 17:22:03
Imagine the following class that manages a resource (my question is only about the move assignment operator): struct A { std::size_t s; int* p; A(std::size_t s) : s(s), p(new int[s]){} ~A(){delete [] p;} A(A const& other) : s(other.s), p(new int[other.s]) {std::copy(other.p, other.p + s, this->p);} A(A&& other) : s(other.s), p(other.p) {other.s = 0; other.p = nullptr;} A& operator=(A const& other) {A temp = other; std::swap(*this, temp); return *this;} // Move assignment operator #1 A& operator=(A&& other) { std::swap(this->s, other.s); std::swap(this->p, other.p); return *this; } // Move

Does it make sense to reuse destructor logic by using std::swap in a move assignment operator?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 02:17:05
Consider the following: class Example : boost::noncopyable { HANDLE hExample; public: Example() { hExample = InitializeHandle(); } ~Example() { if (hExample == INVALID_HANDLE_VALUE) { return; } FreeHandle(hExample); } Example(Example && other) : hExample(other.hExample) { other.hExample = INVALID_HANDLE_VALUE; } Example& operator=(Example &&other) { std::swap(hExample, other.hExample); //? return *this; } }; My thinking here is that the destructor will be running on "other" shortly, and as such I don't have to implement my destructor logic again in the move assignment operator by using swap.

Move assignment operator and `if (this != &rhs)`

跟風遠走 提交于 2019-11-26 02:57:57
In the assignment operator of a class, you usually need to check if the object being assigned is the invoking object so you don't screw things up: Class& Class::operator=(const Class& rhs) { if (this != &rhs) { // do the assignment } return *this; } Do you need the same thing for the move assignment operator? Is there ever a situation where this == &rhs would be true? ? Class::operator=(Class&& rhs) { ? } Howard Hinnant Wow, there is just so much to clean up here... First, the Copy and Swap is not always the correct way to implement Copy Assignment. Almost certainly in the case of dumb_array ,

Move assignment operator and `if (this != &rhs)`

痴心易碎 提交于 2019-11-26 01:12:10
问题 In the assignment operator of a class, you usually need to check if the object being assigned is the invoking object so you don\'t screw things up: Class& Class::operator=(const Class& rhs) { if (this != &rhs) { // do the assignment } return *this; } Do you need the same thing for the move assignment operator? Is there ever a situation where this == &rhs would be true? ? Class::operator=(Class&& rhs) { ? } 回答1: Wow, there is just so much to clean up here... First, the Copy and Swap is not

Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

让人想犯罪 __ 提交于 2019-11-25 22:36:34
问题 I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator. I recollect there were some rules, but I don\'t remember, and also can\'t find a reputable resource online. Can anyone help? 回答1: In the following, "auto-generated" means "implicitly declared as defaulted, but not defined as deleted". There are situations where the special member functions are declared, but defined as deleted. The