17.9.3 Conversion operators

原创 2006年05月21日 02:50:00
A conversion operator declaration introduces a user-defined conversion (§13.
4), which augments the predefined
implicit and explicit conversions.
A conversion operator declaration that includes the implicit keyword
introduces a user-defined implicit
conversion. Implicit conversions can occur in a variety of situations,
including function member invocations, cast
expressions, and assignments. This is described further in §13.1.
Chapter 17 Classes
A conversion operator declaration that includes the explicit keyword
introduces a user-defined explicit
conversion. Explicit conversions can occur in cast expressions, and are
described further in §13.2.
A conversion operator converts from a source type, indicated by the
parameter type of the conversion operator, to
a target type, indicated by the return type of the conversion operator. A
class or struct is permitted to declare a
conversion from a source type S to a target type T provided all of the
following are true:
? S and T are different types.
? Either S or T is the class or struct type in which the operator
declaration takes place.
? Neither S nor T is object or an interface-type.
? T is not a base class of S, and S is not a base class of T.
From the second rule it follows that a conversion operator must convert
either to or from the class or struct type
in which the operator is declared. [Example: For example, it is possible
for a class or struct type C to define a
conversion from C to int and from int to C, but not from int to bool. end
It is not possible to redefine a pre-defined conversion. Thus, conversion
operators are not allowed to convert from
or to object because implicit and explicit conversions already exist
between object and all other types.
Likewise, neither the source nor the target types of a conversion can be a
base type of the other, since a
conversion would then already exist.
User-defined conversions are not allowed to convert from or to
interface-types. In particular, this restriction
ensures that no user-defined transformations occur when converting to an
interface-type, and that a conversion to
an interface-type succeeds only if the object being converted actually
implements the specified interface-type.
The signature of a conversion operator consists of the source type and the
target type. (This is the only form of
member for which the return type participates in the signature.) The
implicit or explicit classification of a
conversion operator is not part of the operator?s signature. Thus, a class
or struct cannot declare both an
implicit and an explicit conversion operator with the same source and
target types.
[Note: In general, user-defined implicit conversions should be designed to
never throw exceptions and never lose
information. If a user-defined conversion can give rise to exceptions (for
example, because the source argument
is out of range) or loss of information (such as discarding high-order
bits), then that conversion should be defined
as an explicit conversion. end note]
[Example: In the example
using System;
public struct Digit
byte value;
public Digit(byte value) {
if (value < 0 || value > 9) throw new ArgumentException();
this.value = value;
public static implicit operator byte(Digit d) {
return d.value;
public static explicit operator Digit(byte b) {
return new Digit(b);
the conversion from Digit to byte is implicit because it never throws
exceptions or loses information, but the
conversion from byte to Digit is explicit since Digit can only represent a
subset of the possible values of a
byte. end example]


const类型的指针可以指向非const类型的对象,但const类型的对象必须由const类型的指针指定: // C++ 语言强制要求指向 const 对象的指针也必须具有 const 特...
  • amber2012
  • amber2012
  • 2013年05月20日 18:23
  • 3071


先看个例子: public class Test { public static void main(String[] args) { char c=1; c=c+3; ...
  • zxz_tsgx
  • zxz_tsgx
  • 2014年04月20日 14:42
  • 2895


安装高版本ruby源,sudo gem install -n /usr/local/bin cocoa pods后提示的错误
  • SharkToping
  • SharkToping
  • 2016年08月25日 09:30
  • 9035


Python网络爬虫简明教程 通过本教程可以快速了解网络爬虫过程,对一般性数据抓取有启发意义。...
  • ziyuanxiazai123
  • ziyuanxiazai123
  • 2015年01月06日 22:57
  • 4507


Conversion库包含有一些函数,它们是现有的强制类型转换操作符(static_cast, const_cast, 和  dynamic_cast)的增强。Conversion为安全的多...
  • pud_zha
  • pud_zha
  • 2014年07月10日 14:37
  • 631

每周学一点 egret(2): EgretConversion 工具转换ts

今晚无聊试了一下wing的格式化和这个转换工具。开始的时候我尝试手写翻译,发现这一款转换也比较简单。所以尝试做了一下转换。 对于如果文件名是中文 要小心一点。总是出现怪怪的。转换后,没有直接跳转...
  • hero82748274
  • hero82748274
  • 2015年08月19日 22:44
  • 1808


赋值 Assignment (=) 式子从右到左执行。 right-to-left rule). #include using namespace std; class A; cl...
  • love_hot_girl
  • love_hot_girl
  • 2013年10月16日 16:33
  • 514


这次研究一下rx是如何实现线程切换的 参考: http://gank.io/post/560e15be2dca930e00da1083 http://www.jianshu.com/p/d...
  • lxj1137800599
  • lxj1137800599
  • 2017年04月24日 16:04
  • 217

Xcode 7新的特性:Nullability,__kindof修饰符 _Nonlable, _Nonnull(温故知新,防止遗忘)

1 关于  使用_Nonlable, _Nonnull  在于调试和编程的时候的快速提示错误。 Xcode 7 包含了一个名为 “Apple LLVM 7.0 - Warnings - All ...
  • zxc110110
  • zxc110110
  • 2015年12月22日 14:55
  • 3074


1、升级sdk后遇到的问题:Unable to execute dex: java.nio.BufferOverflowException. 具体信息如下: Unable to execute dex...
  • ajupiter
  • ajupiter
  • 2013年12月04日 17:49
  • 2911
您举报文章:17.9.3 Conversion operators