发新话题
打印

8 D语言 attributes

8 D语言 attributes

知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070418

点击下面网址查看原文:
http://www.d-programming-language-china.org

by: uFramer D语言论坛 http://www.d-programming-language-china.org
from: http://www.digitalmars.com/d/attribute.html
version: 基于D 1.012 (Apr 12, 2007)

QUOTE:
attribute指示器:
    attribute :
    attribute 声明定义块

attribute:
    链接attribute
    对齐attribute
    编译器指令
    deprecated
    private
    package
    protected
    public
    export
    static
    final
    override
    abstract
    const
    auto
    scope

声明定义块
    声明定义
    { }
    { 声明定义 }


AttributeSpecifier:
    Attribute :
    Attribute DeclarationBlock

Attribute:
    LinkageAttribute
    AlignAttribute
    Pragma
    deprecated
    private
    package
    protected
    public
    export
    static
    final
    override
    abstract
    const
    auto
    scope

DeclarationBlock
    DeclDef
    { }
    { DeclDefs }

attribute用来修饰所声明的一个或多个方法。通常的形式是:

attribute declaration;    // 影响这个声明

attribute:        // 影响直到下一个‘}’的所有声明
    declaration;
    declaration;
    ...

attribute            // 影响语句块中所有的声明
{
    declaration;
    declaration;
    ...
}

对于出现在可选的 else 子句中的声明:

attribute
    declaration;
else
    declaration;

attribute            // 影响语句块中所有的声明
{
    declaration;
    declaration;
    ...
}
else
{
    declaration;
    declaration;
    ...
}

链接attribute

QUOTE:
链接attribute:
    extern
    extern ( 链接类型 )

链接类型:
    C
    D
    Windows
    Pascal

D 提供了一种方便的调用 C 函数和操作系统 API 函数的方法,因为对这两者的兼容都是必要的。链接类型 是大小写敏感的,并且可以由实现扩展(它们不是关键字)。C 和 D 是必须提供的,其他的含义由实现定义。C++保留将来使用。实现注记:对于 Win32 平台来说,应该支持 Windows 和 Pascal 。
通过下面的方式指定 C 函数调用约定:

extern (C):
    int foo();    // 使用 C 约定调用 foo()

D 调用约定是:

extern (D):

或者:

Windows API 调用约定是:( 本文出处: http://www.d-programming-language-china.org )

extern (Windows):
    void *VirtualAlloc(
    void *lpAddress,
    uint dwSize,
    uint flAllocationType,
    uint flProtect
    );

对齐attribute

QUOTE:
对齐attribute:
    align
    align ( 整数 )

AlignAttribute:
    align
    align ( Integer )

指定结构成员如何对齐。如果只有 align ,则设为默认值,即使用同对应的 C 编译器相同的默认对齐值。整数 指定了对齐的值,它的行为同对应的 C 编译器使用非默认的对齐值时相同。

Matching the behavior of the companion C compiler can have some surprising results, such as the following for Digital Mars C++:

struct S
{    align(4) byte a;    // placed at offset 0
    align(4) byte b;    // placed at offset 1
}

AlignAttribute is meant for C ABI compatiblity, which is not the same thing as binary compatibility across diverse platforms. For that, use packed structs:

align (1) struct S
{    byte a;    // placed at offset 0
    byte[3] filler1;
    byte b;    // placed at offset 4
    byte[3] filler2;
}

如果值为 1 就表示不进行对齐;成员将紧挨在一起。

Do not align references or pointers that were allocated using NewExpression on boundaries that are not a multiple of size_t. The garbage collector assumes that pointers and references to gc allocated objects will be on size_t byte boundaries. If they are not, undefined behavior will result.( 本文出处: http://www.d-programming-language-china.org )

应用到非结构或结构成员的对齐属性将被忽略。

废弃attribute

我们经常会将库中的某一attribute标记为废弃,同时仍然保留它以保持向后兼容。这类声明可以被标记为废弃,这意味着可以通过设置编译器选项,在遇到引用这类声明的代码时产生错误:

deprecated
{
    void oldFoo();
}

实现注记: 编译器应该有一个选项用来指定是否被废弃的attribute可以通过编译。

保护attribute

用于保护的attribute有 private、 package、 protected、 public 和 export 。
private 意味着只有本类内的及本类所在模块内的成员可以访问被保护的成员。私有成员不能被重写。私有模块成员等价于 C 程序中的 static 声明。

package 扩展了 private 的访问权,这样包中的成员就可以访问位于同一包内的其他的模块内的成员。如果包是嵌套的话,这条规则只应用于最内层的包。( 本文出处: http://www.d-programming-language-china.org )

protected 意味着只有本类内或者派生类内的成员或者本类所在模块内的成员可以访问被保护的成员。
If accessing a protected instance member through a derived class member function, that member can only be accessed for the object instance which is the 'this' object for the member function call.
protected 不能用于模块成员。

public 意味着可执行程序中的任何代码都可以访问这个成员。( 本文出处: http://www.d-programming-language-china.org )

export 意味着可执行程序外的任何代码都可以访问这个成员。export 的意思也就是从 DLL 中导出定义。

Const attribute

QUOTE:
const

const attribute声明可以在编译时估值的常量。例如:

const int foo = 7;

const
{
    double bar = foo + 6;
}

A const declaration without an initializer must be initialized in a constructor (for class fields) or in a static constructor (for static class members, or module variable declarations).( 本文出处: http://www.d-programming-language-china.org )

const int x;
const int y;

static this()
{
    x = 3;    // ok
    // error: y not initialized
}

void foo()
{
    x = 4;    // error, x is const and not in static constructor
}

class C
{
    const int a;
    const int b;
    static const int c;
    static const int d;

    this()
    {    a = 3;        // ok
    a = 4;        // ok, multiple initialization allowed
    C p = this;
    p.a = 4;    // error, only members of this instance
    c = 5;        // error, should initialize in static constructor
    // error, b is not initialized
    }

    this(int x)
    {
    this();        // ok, forwarding constructor
    }

    static this()
    {
    c = 3;        // ok
    // error, d is not initialized
    }
}

It is not an error to have const module variable declarations without initializers if there is no constructor. This is to support the practice of having modules serve only as declarations that are not linked in, the implementation of it will be in another module that is linked in.

Override attribute

QUOTE:
override

override attribute用于虚函数。这意味着必须用具有同基类中相同的名字和参数覆盖这个函数。当某个基类的成员函数的参数变化的时候,override attribute可以用来捕获错误。这样所有派生类会得知必须更新它们的覆盖函数。

class Foo
{
    int bar();
    int abc(int x);
}

class Foo2 : Foo
{
    override
    {
    int bar(char c);        // 错误,Foo 中没有 bar(char)
    int abc(int x);        // ok
    }
}

Static attribute

QUOTE:
static

static attribute用于函数和数据。这意味着这个声明不会应用于一个对象的特定实例,而应用于对象的类型。换句话说,这意味着没有 this 引用。当用于其他声明时,static 会被忽略。

class Foo
{
    static int bar() { return 6; }
    int foobar() { return 7; }
}

...

Foo f = new Foo;
Foo.bar();    // 得到 6
Foo.foobar();    // 错误,没有 Foo 的实例
f.bar();        // 得到 6
f.foobar();    // 得到 7

静态函数决不会是虚的。
在整个程序中,静态数据只有一个实例,而不是每个对象有一个。

static 与在 C 中不同,它并不同时意味着它的作用域局限在本文件中。你可以使用 private attribute做到这一点。例如:( 本文出处: http://www.d-programming-language-china.org )

module foo;
int x = 3;        // x 是全局的
private int y = 4;        // y 是 foo 中局部的

static 可以被应用于构造函数和析构函数,这样就得到静态构造函数和静态析构函数。

Auto attribute

QUOTE:
auto

The auto attribute is used when there are no other attributes and type inference is desired.

auto i = 6.8;    // declare i as a double

Scope attribute

scope attribute 用于局部变量和类声明。对于类声明来说,scope attribute创建了一个scope 类。对于局部变量来说,scope 实现了RAII(资源获得即初始化Resource Acquisition Is initialization)。也就是说,当指向对像的引用脱离作用域时将自动调用它的析构函数。即使是由于抛出了异常而脱离作用域也会调用析构函数,因此 scope 用来保证清除。

同一个指针的多个变量脱离作用域时,将按变量构造时的相反顺序调用析构函数。

scope 不能被用于全局的、静态的数据成员或者用 reft 或 out 修饰的参数。scope 的数组是非法的,scope用于函数返回值也不允许的。如果不是初始化的话,是不允许对 scope赋值的。注记:如果将来出现了某种强制性的要求,这些限制可能会放松。( 本文出处: http://www.d-programming-language-china.org )

Abstract Attribute

If a class is abstract, it cannot be instantiated directly. It can only be instantiated as a base class of another, non-abstract, class.

Classes become abstract if they are defined within an abstract attribute, or if any of the virtual member functions within it are declared as abstract.

Non-virtual functions cannot be declared as abstract.

Functions declared as abstract can still have function bodies. This is so that even though they must be overridden, they can still provide 'base class functionality.'

( lastupdate:20070421 最新文章请访问http://www.d-programming-language-china.org )

关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台

TOP

发新话题