开发者

what is the difference between user-defined conversion and user-defined operator?

开发者 https://www.devze.com 2023-02-14 19:00 出处:网络
In the context of oper开发者_运维问答ator overloading, what is the difference between user-defined conversion and user-defined operator?A user-defined conversion is either:

In the context of oper开发者_运维问答ator overloading, what is the difference between user-defined conversion and user-defined operator?


A user-defined conversion is either:

  • A constructor in the destination type which can be called with a single parameter of the source type (more arguments can exist if they have default values)

or

  • A non-static member function of the source type with the name operator DESTTYPE()

As you can see, the second option uses the operator keyword just as is used when overloading the traditional operators.

(Note: All of this is formally described in section [class.conv] of the C++ standard.)


In C++, there is no concept of user-defined operators that can be overloadable. Only existing operators, with an exception of few( ., .*, ::, ?:, sizeof ), can be overloaded.


"User-defined conversions allow you to specify object conversions with constructors or with conversion functions. User-defined conversions are implicitly used in addition to standard conversions for conversion of initializers, functions arguments, function return values, expression operands, expressions controlling iteration, selection statements, and explicit type conversions."

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr383.htm

I'm not sure that user-defined operators are available (technically) in C++, but operators can be overloaded.

http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号