知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070423
点击下面网址查看原文:
http://www.d-programming-language-china.org
by: uFramer D语言论坛 http://www.d-programming-language-china.org
from: http://www.digitalmars.com/d/struct.html
version: 基于D 1.013 (Apr 19, 2007)
Whereas classes are reference types, structs are value types. Any C struct can be exactly represented as a D struct. In C++ parlance, a D struct is a POD (Plain Old Data) type, with a trivial constructors and destructors. Structs and unions are meant as simple aggregations of data, or as a way to paint a data structure over hardware or an external type. External types can be defined by the operating system API, or by a file format. Object oriented features are provided with the class data type.
A struct is defined to not have an identity; that is, the implementation is free to make bit copies of the struct as convenient.
Struct, Class Comparison Table Feature struct class C struct C++ struct C++ class
Struct, Class Comparison Table | Feature | struct | class | C struct | C++ struct | C++ class |
| value type | X | | X | X | X |
| reference type | | X | | | |
| data members | X | X | X | X | X |
| hidden members | | X | | X | X |
| static members | X | X | | X | X |
| default member initializers | X | X | | | |
| bit fields | | | X | X | X |
| non-virtual member functions | X | X | | X | X |
| virtual member functions | | X | | X | X |
| constructors | | X | | X | X |
| destructors | | X | | X | X |
| RAII | | X | | X | X |
| operator overloading | X | X | | X | X |
| inheritance | | X | | X | X |
| invariants | X | X | | | |
| unit tests | X | X | | | |
| synchronizable | | X | | | |
| parameterizable | X | X | | X | X |
| alignment control | X | X | | | |
| member protection | X | X | | X | X |
| default public | X | X | X | X | |
| tag name space | | | X | X | X |
| anonymous | X | | X | X | X |
结构的声明:
AggregateDeclaration:
Tag { DeclDefs }
Tag Identifier StructBody
Tag Identifier ;
Tag:
struct
union
StructBody:
{ }
{ StructBodyDeclarations }
StructBodyDeclarations:
StructBodyDeclaration
StructBodyDeclaration StructBodyDeclarations
StructBodyDeclaration:
Declaration
StaticConstructor
StaticDestructor
Invariant
UnitTest
StructAllocator
StructDeallocator
StructAllocator:
ClassAllocator
StructDeallocator:
ClassDeallocator
它们同在 C 中的结构基本一样,除了以下一些例外:
没有位域
可以显式指定对齐
没有单独的标记名字空间 —— 标记名并入当前的作用域
如下面这样的声明:
不允许这样使用:( 本文出处: http://www.d-programming-language-china.org )
匿名结构/联合可以作为其他结构/联合的成员存在
可以为成员提供默认初始值
可以拥有成员函数和静态成员
Static Initialization of Structs结构的静态初始化
如果不提供初始值,静态结构成员会被默认地初始化为成员类型的初始值。如果提供了静态初始值,成员会按照这样的语法初始化:成员名,冒号,表达式。成员可以按照任意顺序初始化。没有出现在初始化列表中的成员会被默认值初始化。( 本文出处: http://www.d-programming-language-china.org )
struct X { int a; int b; int c; int d = 7;}
static X x = { a:1, b:2}; // c 被设置为 0, d 为 7
static X z = { c:4, b:5, a:2 , d:5}; // z.a = 2, z.b = 5, z.c = 4, z.d = 5
C 风格的初始化,依赖于结构声明中的成员的顺序,也是被支持的:
static X q = { 1, 2 }; // q.a = 1, q.b = 2, q.c = 0, q.d = 7
Static Initialization of Unions联合的静态初始化
联合需要显式地初始化。
union U { int a; double b; }
static U u = { b : 5.0 }; // u.b = 5.0
如果联合的其他成员占用的存储空间更大,剩下的部分被初始化为零。( 本文出处: http://www.d-programming-language-china.org )
Dynamic Initialization of Structs结构的动态初始化
结构可以被同类型值动态初始化:
struct S { int a; }
S t; // default initialized
t.a = 3;
S s = t; // s.a is set to 3
If opCall is overridden for the struct, and the struct is initialized with a value that is of a different type, then the opCall operator is called:
struct S
{ int a;
static S opCall(int v)
{ S s;
s.a = v;
return s;
}
static S opCall(S v)
{ S s;
s.a = v.a + 1;
return s;
}
}
S s = 3; // sets s.a to 3
S t = s; // sets t.a to 3, S.opCall(s) is not called
Struct Properties结构属性
.sizeof 以字节为单位的结构大小
.alignof 要对齐到的边界值
.tupleof Gets type tuple of fields
Struct Field Properties结构域属性
.offsetof 从结构开始的字节偏移量
of struct
( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台