yidabu 2007-4-21 11:32
6 D语言 类型 types
6 D语言 类型 types知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070417点击下面网址查看原文:http://www.d-programming-language-china.org by: uFramer D语言论坛 http://www.d-programming-language-china.org from: http://www.digitalmars.com/d/type.html version: 基于D 1.012 (Apr 12, 2007) Keyword Description Default Initializer (.init) void no type - bool boolean value false byte signed 8 bits 0 ubyte unsigned 8 bits 0 short signed 16 bits 0 ushort unsigned 16 bits 0 int signed 32 bits 0 uint unsigned 32 bits 0 long signed 64 bits 0L ulong unsigned 64 bits 0L cent signed 128 bits (reserved for future use) 0 ucent unsigned 128 bits (reserved for future use) 0 float 32 bit floating point float.nan double 64 bit floating point double.nan real largest hardware implemented floating point size (Implementation Note: 80 bits for Intel CPUs) real.nan ifloat imaginary float float.nan * 1.0i idouble imaginary double double.nan * 1.0i ireal imaginary real real.nan * 1.0i cfloat a complex number of two float values float.nan + float.nan * 1.0i cdouble complex double double.nan + double.nan * 1.0i creal complex real real.nan + real.nan * 1.0i char unsigned 8 bit UTF-8 0xFF wchar unsigned 16 bit UTF-16 0xFFFF dchar unsigned 32 bit UTF-32 0x0000FFFF 派生数据类型 指针 数组 联合数组 函数 委托用户定义数据类型 别名 typedef 枚举 结构 联合 类基本类型 The base type of an enum is the type it is based on: [Copy to clipboard] [ - ]CODE: enum E : T { ... } // T is the base type of E The base type of a typedef is the type it is formed from: [Copy to clipboard] [ - ]CODE: typedef T U; // T is the base type of U 指针转换 D 允许指针到非指针、非指针到指针的转换,但是,决不要对指向由垃圾收集程序分配的数据的指针进行这样的操作。隐式转换 D 有许多类型,有一些是内建的,另一些是派生的。如果每次类型转换都需要显式转型的话会是很麻烦的事,所以我们用隐式转换来自动处理那些明显的转型。 使用 typedef 声明的类型可以被转换为它所代表的类型,但是反过来就必须进行显式转型。例如:( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: typedef int myint; int i; myint m; i = m; // OK m = i; // 错误 m = cast(myint)i; // OK 整数提升 下面的类型会被隐式提升 : QUOTE: from to bool int byte int ubyte int short int ushort int char int wchar int dchar uint If a typedef or enum has as a base type one of the types in the left column, it is converted to the type in the right column.常用算数转换 常见的算术转换会将二元运算符的操作数转换为通用的类型。这个操作数必须已经是算术类型。下面的规则将会按顺序应用,请参考 base type: 1 如果有操作数是 real ,另一个操作数会被转换为 real 2 如果一个操作数是 double ,另一个操作数会被转换为 double 3 如果一个操作数是 float ,另一个操作数会被转换为 float 4 对每个操作数应用整数提升,然后: 1.1 如果两个操作数类型相同,无需再作转换 1.2 如果两个操作数都是有符号或无符号的,较小的类型会被转换为较大的类型。 1.3 如果有符号的类型比无符号的类型大,无符号的类型会被转换为有符号的类型。 1.4 否则有符号的类型会被转换为无符号的类型。 If one or both of the operand types is a typedef or enum after undergoing the above conversions, the result type is:( 本文出处: http://www.d-programming-language-china.org ) 1 If the operands are the same type, the result will be the that type. 2 If one operand is a typedef or enum and the other is the base type of that typedef or enum, the result is the base type. 3 If the two operands are different typedefs or enums but of the same base type, then the result is that base type. Integer values cannot be implicitly converted to another type that cannot represent the integer bit pattern after integral promotion. For example:( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: ubyte u1 = cast(byte)-1; // error, -1 cannot be represented in a ubyte ushort u2 = cast(short)-1; // error, -1 cannot be represented in a ushort uint u3 = cast(int)-1; // ok, -1 can be represented in a uint ulong u4 = cast(ulong)-1; // ok, -1 can be represented in a ulong 浮点型指针不能隐式转换成整数型。 复数浮点指针型不能隐式转换为非复数浮点指针型。 虚浮点指针型不能隐式转换成浮点,double或者实数型。 浮点,double,实数型不能隐式转抽象成虚浮点指针型。bool 一字节长度的bool型只能表示true 或者 false。只能接受以下操作符: QUOTE: & | ^ &= |= ^= ! && || ?: bool值能隐式转换成任意整型。false 转换成0,true转换成1。 数字文字量0和1能隐式转换成bool值false 和true。 把表达式转换成boole意味着,测试算术运行型是0或者!=0,测试指针或引用是null或者!=null委托 D 中没有成员指针,但支持一个更为有用的概念—— 委托。委托是两块数据的聚集:一个对象的引用和一个函数指针。当调用函数时,对象引用构造 this 指针。 委托的声明同函数指针的声明很像,差别是关键字 delegate 替代了 (*) ,并且随后跟着标志符:( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: int function(int) fp; // fp 是指向函数的指针 int delegate(int) dg; // dg 是函数的委托 C 风格的声明函数指针的语法也被支持: [Copy to clipboard] [ - ]CODE: int (*fp)(int); // fp 是指向函数的指针 委托同函数指针一样被初始化: [Copy to clipboard] [ - ]CODE: int func(int); fp = &func; // fp 指向 func class OB { int member(int); } OB o; dg = &o.member; // dg 是 object o 的成员函数 // member 的委托 委托不能用静态成员函数或者非成员函数初始化。 委托同函数指针一样调用: [Copy to clipboard] [ - ]CODE: fp(3); // 调用 func(3) dg(3); // 调用 o.member(3) ( lastupdate:20070421 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台