All C++ operators that I have worked with return something, for example the +
operator returns the result of the addition.
Do all C++ operators return something, or are there some C++ operators that do not return anything?
The assignment operators in C and C++ return the value of the variable being assigned to, i.e., their left operand. In your example of a = b , the value of this entire expression is the value that is assigned to a (which is the value of b converted into the type of a ).
The assignment operators return the value of the object specified by the left operand after the assignment. The resultant type is the type of the left operand. The result of an assignment expression is always an l-value.
Strictly speaking, the result of a copy assignment operator doesn't need to return a reference, though to mimic the default behavior the C++ compiler uses, it should return a non-const reference to the object that is assigned to (an implicitly generated copy assignment operator will return a non-const reference - C++03 ...
In short: The new operator returns the unique address of the allocated object. When you allocate an array of objects the address of the first object is returned.
Although they are probably not exactly what you are thinking about, note that the delete
and delete[]
C++ 'keywords' are actually operators; and they are defined as having the void
return type - which means they evaluate to nothing (which is not 'something').
From cppreference:
void operator delete ( void* ptr ) noexcept; void operator delete[]( void* ptr ) noexcept;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With