ice slice Forward Declarations



Both interfaces andclasses can be forward declared. Forward declarations permit the creation of mutually dependent objects, for example:

接口和类可以都可以先被声明。(前置)声明允许相互依赖的对象被创建 例如:

<span style="font-size:14px;">Slice

module Family {
    interface Child;            // Forward declaration

    sequence<Child*> Children;  // OK

    interface Parent {
        Children getChildren(); // OK
    };

    interface Child {           // Definition
        Parent* getMother();
        Parent* getFather();
    };
};
</span>

Without the forward declaration of Child, the definition obviously could not compile becauseChild andParent are mutually dependent interfaces. You can use forward-declared interfaces and classes to define types (such as theChildren sequence in the previous example). Forward-declared interfaces and classes are also legal as the type of a structure, exception, or class member, as the value type of a dictionary, and as the parameter and return type of an operation. However, you cannot inherit from a forward-declared interface or class until after its definition has been seen by the compiler

如果没有先声明Child,那么这个定义显示不能编译, 因为Child和Parent是相互依赖的接口(Parent依赖Child),为了定义某个类型你可以提前声明某个类或接口(例如上个例子中的 Children sequence)。前置声明的接口/类是合法的,作为structure exeption 或 class member 类型,作为dictionary的值类型,作为方法的参数/返回值, 但是, 你不能继承一个前置声明的接口/类, 除非编译器已经有了它的定义

Slice

interface Base;                         // Forward declaration

interface Derived1 extends Base {};     // Error!

interface Base {};                      // Definition

interface Derived2 extends Base {};     // OK, definition was seen

Not inheriting from a forward-declared base interface or class until its definition is seen is necessary because, otherwise, the compiler could not enforce that derived interfaces must not redefine operations that appear in base interfaces.

不能继承前置声明的类/接口知道它被定义是有必要的, 否则编译器不能确认子类/接口该如何重载基类/接口的方法。(这个和c++ 等语言一样)

注: A multi-pass compiler 是可以的,但增加的复杂性是不值得的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值