首页 > 解决方案 > 使用过程类范围的输出参数作为函数返回值

问题描述

   type T is abstract tagged null record;
   type T1 is new T with null record;
   
   procedure Get_Value (Value : out T'Class) is
      T1_Value : T1 := (null record);
   begin
      Value := T'Class (T1_Value);
   end;
   
   function Result return T'Class is
      T_Class_Value : T'Class; -- Initialization required
   begin
      Get_Value (T_Class_Value);
      return T_Class_Value;
   end Result;

在此代码中,T_Class_Value 需要初始化。如何从 Get_Value 参数初始化 T_Class_Value?

标签: returnargumentsadaout

解决方案


with Ada.Text_Io; use ADa.Text_IO;

procedure Main is
   type T is abstract tagged null record;
   type T1 is new T with null record;
   
   function Result return T1'Class is
      T1_Value : t1 := (Null record);
   begin
      return T1'Class(T1_Value);
   end Result;
   
   T1_Class : T1'Class := Result;
   
begin
   Put_Line("It works");
end Main;

推荐阅读