Many languages do choose the route of making assignment a statement rather than an expression, including Python:
Other languages don't have assignment, but rather scoped bindings, e.g. OCaml:
However, is an expression itself.
The advantage of allowing assignment is that we can directly check the return value of a function inside the conditional, e.g. in this Perl snippet:
Perl additionally scopes the declaration to that conditional only, which makes it very useful. It will also warn if you assign inside a conditional without declaring a new variable there – will warn, will not.
Making the assignment in another statement is usually sufficient, but can bring scoping problems:
Golang heavily relies on return values for error checking. It therefore allows a conditional to take an initialization statement:
Other languages use a type system to disallow non-boolean expressions inside a conditional:
Of course that fails when using a function that returns a boolean.
We now have seen different mechanisms to defend against accidental assignment:
- Disallow assignment as an expression
- Use static type checking
- Assignment doesn't exist, we only have bindings
- Allow an initialization statement, disallow assignment otherwise
- Disallow assignment inside a conditional without declaration
I've ranked them in order of ascending preference – assignments inside expressions can be useful (and it's simple to circumvent Python's problems by having an explicit declaration syntax, and a different named argument syntax). But it's ok to disallow them, as there are many other options to the same effect.
Bug-free code is more important than terse code.
answered Feb 13 '14 at 12:48
In the C++programming language, the assignment operator, , is the operator used for assignment. Like most other operators in C++, it can be overloaded.
The copy assignment operator, often just called the "assignment operator", is a special case of assignment operator where the source (right-hand side) and destination (left-hand side) are of the same class type. It is one of the special member functions, which means that a default version of it is generated automatically by the compiler if the programmer does not declare one. The default version performs a memberwise copy, where each member is copied by its own copy assignment operator (which may also be programmer-declared or compiler-generated).
The copy assignment operator differs from the copy constructor in that it must clean up the data members of the assignment's target (and correctly handle self-assignment) whereas the copy constructor assigns values to uninitialized data members. For example:
Return value of overloaded assignment operator
The language permits an overloaded assignment operator to have an arbitrary return type (including ). However, the operator is usually defined to return a reference to the assignee. This is consistent with the behavior of assignment operator for built-in types (returning the assigned value) and allows for using the operator invocation as an expression, for instance in control statements or in chained assignment. Also, the C++ Standard Library requires this behavior for some user-supplied types.
Overloading copy assignment operator
When deep copies of objects have to be made, exception safety should be taken into consideration. One way to achieve this when resource deallocation never fails is:
- Acquire new resources
- Release old resources
- Assign the new resources' handles to the object
However, if a no-fail (no-throw) swap function is available for all the member subobjects and the class provides a copy constructor and destructor (which it should do according to the rule of three), the most straightforward way to implement copy assignment is as follows:
Assignment between different classes
C++ supports assignment between different classes, both via implicit copy constructor and assignment operator, if the destination instance class is the ancestor of the source instance class:
Copying from ancestor to descendant objects, which could leave descendant's fields uninitialized, is not permitted.
- ^Stroustrup, Bjarne (2000). The C++ Programming Language (3 ed.). Addison-Wesley. p. 244. ISBN 978-0-201-70073-2.
- ^Working Draft, Standard for Programming Language C++, Section 126.96.36.199, Table 23; http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
- ^Sutter, H.; Alexandrescu, A. (October 2004), C++ Coding Standards, Addison-Wesley, ISBN 0-321-11358-6