【问题标题】:Ada. Why is <<procedurename.all>> required to call a procedure from a pointer?艾达。为什么需要 <<procedurename.all>> 从指针调用过程?
【发布时间】:2017-04-16 05:21:51
【问题描述】:

我对 Ada 很陌生,我想知道为什么我必须编写 Acc.all 或 d.all 来通过指针调用过程。我认为 .all 主要用于记录,因为这样您可以将 .all 记录值设置为等于其他记录值。同时我想知道为什么我在调用函数时不需要这样做。这是我的代码供参考。请忘记 AnimalPKG.Animals(Total, 3,2,1),因为我只是在练习如何导入包。

    with AnimalPKG, Ada.Text_IO, Ada.Integer_Text_IO; 
    use Ada.Text_IO, Ada.Integer_Text_IO; 

    procedure Main is

       procedure thisisaccess is
          begin
          Put("thisisaccess"); 

          end thisisaccess;

       Type accessprocedure is access procedure;

       procedure passinfunction(d : accessprocedure) is 
          begin
         d.all; 
       end passinfunction; 

       function times5(a : Integer) return Integer is 
          begin
          return a*5; 
       end times5; 

       Type times5access is access function(x : Integer) return Integer; 

       B : times5access := times5'Access; 

        Acc : accessprocedure;
       Acc2 : accessprocedure;  //Modification

   Total : Integer;


begin

   Acc := thisisaccess'Access; //Modification
   Acc2 := Acc; //Modification
   Acc2.all; //Modification

       AnimalPKG.Animals(Total, 3,2,1); 
       Put(Total); 
       Acc.all; 
       passinfunction(Acc); 
       Put(B(3)); 
    end Main;

【问题讨论】:

    标签: function-pointers ada


    【解决方案1】:

    .all 构造是显式取消引用。编译器无法通过P(属于access procedure 类型)来判断您是指指针 本身还是调用。请注意,在 Ada 中,调用过程时不需要空括号。因此P 被理解为指针本身,P.all 被理解为调用。

    【讨论】:

    • 有道理,谢谢。对原帖中的代码稍作修改,供参考。
    猜你喜欢
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多