comp.lang.ada
 help / color / mirror / Atom feed
* Constructors/Destructors in Ada95
@ 2000-10-18  0:00 Francois Godme
  2000-10-19  0:00 ` Marin David Condic
                   ` (2 more replies)
  0 siblings, 3 replies; 35+ messages in thread
From: Francois Godme @ 2000-10-18  0:00 UTC (permalink / raw)


Hi all,

Where can I find articles, papers, or posted messages explaining me the
reasons for the lack of support for Constructors/Destructors directly in
the language. I have heard that, in 1995, Tucker Taft on c.l.a posted
several messages on this very subject.

I went to www.deja.com but they say that temporarily it is not possible
to consult archives past 1999. Is there an Ada web site which archives
our group?

Thanks.






^ permalink raw reply	[flat|nested] 35+ messages in thread
* Re: Constructors/Destructors in Ada95
@ 2000-10-29 22:51 rwilson007007
  2000-10-30  4:03 ` Ray Blaak
  2000-10-30 12:13 ` Marin David Condic
  0 siblings, 2 replies; 35+ messages in thread
From: rwilson007007 @ 2000-10-29 22:51 UTC (permalink / raw)


Greetings,

One thing that has struck me (vis-a-vis C++ versus Ada classes) is
that, unlike in Ada, the data structure associated with a C++ class is
never explicitly
defined by the language -
that is, afaik, the compiler can choose whatever underlying
represenation for the class it wishes.

If I desired to mimic the C++ notion of classes in Ada, I
would be tempted to encapsulate a tagged record instance within a
generic package
(the formal parameters of which act as constructor arguments and the
elaboration of which acts as a constructor). This both hides the
ada-record data representation and renders the familiar C++
"object_ref.method_name" style of method invocation syntax.

That is, rather than being tempted to overload Ada's assignment
operator, I would prefer to replace:
   "My_Object = new My_Class_Name (Argument_List)" in C++
with
   "package My_Object is new My_Class_Name.Instance (Argument_List)"
in Ada (see example below...).

** Could one of the wise participants in this discussion please
instruct me as to whether or not there is some fundamental problem or
limitation with Ada generics that makes this an unsensible approach?**

--Richard Wilson

-- BRIEF EXAMPLE:
-- SPEC
-- definition of class which inherits from a controlled object
with Some_Base_Class; -- limited controlled
with Attribute_1_Class;
with Attribute_2_Class;
with Attribute_3_Class;

package Some_Class is

   type Object is new Some_Base_Class.Object with
    record
      Attribute_1 : Attribute_1_Class.Object;
      Attribute_2 : Attribute_2_Class.Object;
      Attribute_3 : Attribute_3_Class.Object;
    end record;

   type Ref is access all Object'Class;

   procedure Method_1 (This : Ref; Parameter_1 : in Integer);
   procedure Method_2 (This : Ref; Returns : out
Attribute_1_Class.Object);

   generic
     Attribute_1 : Attribute_1_Class.Object :=
Attribute_1_Class.Default;
     Attribute_2 : Attribute_2_Class.Object;
     Attribute_3 : Attribute_3_Class.Object;

   package Instance is

     procedure Method_1 (Parameter_1 : Integer);
     procedure Method_2 (Returns : out Attribute_1_Class.Object);
     -- note that "This : Ref" argument is not necessary at this scope

     function Copy return Ref; -- returns deep copy
     procedure Destroy;
     -- object is created when Instance first instantiated
     -- if Destroy has been invoked, the instance of Instance is still
     --   in scope,  and and one wishes to use the object again
     --   then one must invoke:
     procedure Resurrect
       (Attribute_1 : in Attribute_1_Class.Object :=
Attribute_1_Class.Default;
        Attribute_2 : in Attribute_2_Class.Object;
        Attribute_3 : in Attribute_3_Class.Object);

     end Instance;

 private
     procedure Initialize (This : in out Object);
     procedure Finalize (This : in out Object);

end Some_Class;

-- BODY

package body Some_Class is

   procedure Method_1 (This : Ref; Parameter_1 :in Integer) is
   begin
      This.Attribute_2.Ticks := This.Attribute_2.Ticks + Parameter_1;
   end Method_1;

   procedure Method_2 (This : Ref; Returns : out
Attribute_1_Class.Object) is
   begin
     return This.Attribute_2;
   end Method_2;

   package body Instance is
     This : aliased Object;

     procedure Method_1 (Parameter_1 : in Integer) is
     begin
       Some_Class.Method_1 (This => This, Parameter_1 => Parameter_1);
     end Method_1;

     -- etc...

   begin
     This := new Object;
     This.Attribute_1 := Attribute_1;
     This.Attribute_2 := Attribute_2;
     This.Attribute_3 := Attribute_3;
     -- and so on...

   end Instance;

end Some_Class;

-- CLIENT CODE EXAMPLE

declare
   Init_Param_0 : Attribute_2_Class.Object := (Ticks => 23, others =>
0);
   Init_Param_1 : Attribute_3_Class.Object := (others => '');
   My_Copy : Some_Class.Ref;

  -- construct my object
  package My_Object is new Some_Class.Instance (Attribute_2 =>
Init_Param_0, Attribute_3 => Init_Param_1);

begin
   My_Object.Method_1 (Parameter_1 => 0);
   My_Object.Method_2 (Returns => Init_Param_0);
   My_Copy := My_Object.Copy_To;
   Some_Class.Method_1 (This => My_Copy, Parameter_1 => 0);
   My_Object.Destroy;
   My_Object.Reconstruct (Attribute_2 => Init_Param_0, Attribute_3 =>
Init_Param_1);
   My_Object.Copy (From => My_Copy);

end;


Sent via Deja.com http://www.deja.com/
Before you buy.



^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2000-10-30 22:03 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-10-18  0:00 Constructors/Destructors in Ada95 Francois Godme
2000-10-19  0:00 ` Marin David Condic
2000-10-19  0:00 ` Ted Dennison
2000-10-19  0:00 ` tmoran
2000-10-19  0:00   ` Francois Godme
2000-10-19  0:00     ` Ted Dennison
2000-10-20  0:00     ` Tucker Taft
2000-10-20  0:00       ` Francois Godme
2000-10-21  0:00         ` Marin David Condic
2000-10-23  0:00       ` Francois Godme
2000-10-24  0:00         ` Ray Blaak
2000-10-25  0:00           ` Francois Godme
2000-10-25  0:00           ` Marin David Condic
2000-10-25  0:00             ` dmitry6243
2000-10-25  0:00               ` Pascal Obry
2000-10-26  0:00                 ` dmitry6243
2000-10-25  0:00               ` mark.biggar
2000-10-26 11:44                 ` dmitry6243
2000-10-26 13:25                   ` Robert A Duff
2000-10-27  8:10                     ` dmitry6243
2000-10-26 17:55                   ` tmoran
2000-10-27  8:10                     ` dmitry6243
2000-10-26 21:31                 ` Tucker Taft
2000-10-27  8:46                   ` dmitry6243
2000-10-27  7:12             ` Ray Blaak
2000-10-27 18:11           ` Francois Godme
2000-10-30 11:36             ` Robert A Duff
2000-10-30 22:03               ` dale
2000-10-22  0:00     ` rwilson007007
2000-10-22  0:00       ` Francois Godme
2000-10-24  0:00         ` rwilson007007
  -- strict thread matches above, loose matches on Subject: below --
2000-10-29 22:51 rwilson007007
2000-10-30  4:03 ` Ray Blaak
2000-10-30 12:13 ` Marin David Condic
2000-10-30 16:39   ` Randy Brukardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox