Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion operator as standalone function

People also ask

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 a conversion function in C++?

You can define a member function of a class, called a conversion function, that converts from the type of its class to another specified type.

What is the return type of conversion operator?

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


The one reason I can think of is to prevent implicit conversions being applied to the thing being cast. In your example, if you said:

 bool( "foo" );

then "foo" would be implicitly converted to a string, which would then have the explicit bool conversion you provided applied to it.

This is not possible if the bool operator is a member function, as implicit conversions are not applied to *this. This greatly reduces the possibilities for ambiguity - ambiguities normally being seen as a "bad thing".