assignment-operator
Why are the conditions for an implicitly-defined move constructor/assignment operator different than for copy operations?
I believe backwards compatibility plays a big part here. If the user defines any of the “Rule of three” functions (copy ctor, copy assignment op, dtor), it can be assumed the class does some internal resource management. Implicitly defining a move constructor could suddenly make the class invalid when compiled under C++11. Consider this example: … Read more
Are there drawbacks to calling the assignment operator in the copy constructor?
Yes, that’s a bad idea. All member variables of user-defined types will be initialized first, and then immediately overwritten. That swap trick is this: Foo& operator=(Foo rhs) // note the copying { rhs.swap(*this); //swap our internals with the copy of rhs return *this; } // rhs, now containing our old internals, will be deleted
Is it possible to write a common function that handles both the copy constructor and copy assignment operator?
Yes. There are two common options. One – which is generally discouraged – is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also, all members … Read more
Can you write a common function that handles both the copy constructor and copy assignment operator?
Yes. There are two common options. One – which is generally discouraged – is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also, all members … Read more
Assignment operator not available in derived class
Every class has at least one assignment operator implicitly defined when we don’t provide one ourselves. And when a member function in a derived class is defined with the same name as a member in the base class, it hides all the base class definitions for that name. You can use a using declaration, but … Read more
C++ why the assignment operator should return a const ref in order to avoid (a=b)=c
(x=y) means x.operator=(y), which returns the object x. Therefore, (x=y)=z means (x.operator=(y)).operator=(z). The expression in parens sets x to y and returns x, and then the outer bit sets x to z. It does not set y to z as you might expect, and as the expression x = y = z does. This behavior … Read more
Python: yield and yield assignment
The assignment syntax (“yield expression”) allows you to treat the generator as a rudimentary coroutine. First proposed in PEP 342 and documented here: https://docs.python.org/2/reference/expressions.html#yield-expressions The client code that is working with the generator can communicate data back into the generator using its send() method. That data is accessible via the assignment syntax. send() will also … Read more
Why cannot a non-member function be used for overloading the assignment operator?
Firstly, it should be noted that this has nothing to do with the operator being implemented as a friend specifically. It is really about implementing the copy-assignment as a member function or as a non-member (standalone) function. Whether that standalone function is going to be a friend or not is completely irrelevant: it might be, … Read more
What is the return type of the built-in assignment operator?
The standard correctly defines the return type of an assignment operator. Actually, the assignment operation itself doesn’t depend on the return value – that’s why the return type isn’t straightforward to understanding. The return type is important for chaining operations. Consider the following construction: a = b = c;. This should be equal to a … Read more