Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does conversion operator return a value?

Tags:

For a class A, an integer conversion operator would look something like;

operator int() //Here we don't specify any return type {     return intValue; } 

How is the above function able to return a value when its return value type appears not to be specified? It doesn't appear to return "anything", but I know it's not void.

How is this meaningful when a return type is not specified?

like image 947
Arun Avatar asked Jan 10 '10 12:01

Arun


People also ask

What is the return type of the conversion operator?

What is the return type of the conversion operator? Explanation: Conversion operator doesn't have any return type not even void. 2.

Can operator function return a value?

Some operators return by value, some by reference. In general, an operator whose result is a new value (such as +, -, etc) must return the new value by value, and an operator whose result is an existing value, but modified (such as <<, >>, +=, -=, etc), should return a reference to the modified value.

What is a conversion operator?

A conversion operator, in C#, is an operator that is used to declare a conversion on a user-defined type so that an object of that type can be converted to or from another user-defined type or basic type. The two different types of user-defined conversions include implicit and explicit conversions.

What is the return value of operator?

Result of assignment operatorsThe 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. These operators have right-to-left associativity.


1 Answers

The return type of operator T() is always T. It's a special case of C++.

It does not use standard function prototype syntax T foo() because 2 functions with the same name differing only by the return type cannot coexist (e.g. int foo() conflicts with double foo()). If this syntax is used then you can only define 1 conversion operator overload, which is undesirable.

like image 52
kennytm Avatar answered Sep 19 '22 12:09

kennytm