Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all C++ operators return something?

Tags:

c++

operators

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?

like image 217
user8240761 Avatar asked Jun 30 '20 12:06

user8240761


People also ask

What does the operator return in C?

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 ).

What is the return type of operator?

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.

What should operator return?

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 ...

What is returned by new operator?

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.


1 Answers

No, not all operators return something.

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; 
like image 102
Adrian Mole Avatar answered Sep 21 '22 12:09

Adrian Mole