|
From: shaunpatterson on 2 Jan 2008 09:31 I am trying to write a constructor that will return an access type to a new object. What I what is something like: procedure Test is Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5); begin null; end Test; Parent.ads: package Parent is type Class is abstract tagged limited null record; type Class_Access is access all Class'Class; end Parent; package Parent.Child is type Class is new Parent.Class with private; function Create (Test_Value : Integer) return Class_Access; -- function Create (Test_Value : Integer) return Class; private type Class is new Parent.Class with record Value : Integer := 0; end record; end Parent.Child; package body Parent.Child is function Create (Test_Value : Integer) return Class_Access is begin return new Class' (Value => Test_Value); -- Works only with Ada 2005... I am stuck using Ada 95 end Create; -- function Create (Test_Value : Integer) return Class is -- begin -- return Class' (Value => Test_Value); -- end Create; end Parent.Child; So, the first create works in Ada2005 with: Test_Pointer : Parent.Class_Access := Parent.Child.Create (5); I have also tried the second Create unsuccessfully with Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5); I don't understand how to initialize limited types and their pointers. Any help? Again, my project currently is tied down to Ada95 (gnat 3.16p) -- Shaun
From: Dmitry A. Kazakov on 2 Jan 2008 09:49 On Wed, 2 Jan 2008 06:31:39 -0800 (PST), shaunpatterson(a)gmail.com wrote: > I am trying to write a constructor that will return an access type to > a new object. > > What I what is something like: > > procedure Test is > > Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5); Test_Pointer := Parent.Child.Create (5); > begin > null; > end Test; > > Parent.ads: > > package Parent is > > type Class is abstract tagged limited null record; > type Class_Access is access all Class'Class; I would not use "access all" unless you need some instances of Class allocated on the stack. But that's aside. > end Parent; > > package Parent.Child is > > type Class is new Parent.Class with private; > > function Create (Test_Value : Integer) return Class_Access; > -- function Create (Test_Value : Integer) return Class; > > private > type Class is new Parent.Class with > record > Value : Integer := 0; > end record; > > end Parent.Child; > > package body Parent.Child is > > function Create (Test_Value : Integer) return Class_Access is > begin > return new Class' (Value => Test_Value); > -- Works only with Ada 2005... I am stuck using Ada 95 Result : Class_Access := new Class; -- Allocation This : Class renames Class (Result.all); -- Downcasting begin This.Value := Test_Value; -- Initialization return Result; > end Create; > > end Parent.Child; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de
From: Robert A Duff on 2 Jan 2008 11:02 shaunpatterson(a)gmail.com writes: > function Create (Test_Value : Integer) return Class_Access is > begin > return new Class' (Value => Test_Value); > -- Works only with Ada 2005... I am stuck using Ada 95 > end Create; Right. Dmitry showed how to write it. > -- function Create (Test_Value : Integer) return Class is > -- begin > -- return Class' (Value => Test_Value); > -- end Create; > > end Parent.Child; > > > So, the first create works in Ada2005 with: > > Test_Pointer : Parent.Class_Access := Parent.Child.Create (5); > > I have also tried the second Create unsuccessfully with > > Test_Pointer : Parent.Class_Access := new Parent.Child.Create (5); That's not the right syntax for an allocator. See RM-4.8(2), or any Ada textbook. (The allocator in the first Create above uses the right syntax for an initialized allocator; Dmitry showed an uninitialized allocator.) In Ada 2005, you can say: Test_Pointer : Parent.Class_Access := new Parent.Child.Class'(Parent.Child.Create (5)); Or if you say "use Parent, Parent.Child;": Test_Pointer : Parent.Class_Access := new Child.Class'(Create (5)); This is illegal in Ada 95. In fact, the second Create function won't work in Ada 95 even without the allocator. You can make it legal by making the type nonlimited. Limited types are rather limited (so to speak) in Ada 95, because you're not allowed to explicitly initialize objects (as the above allocator does). This annoying restriction is removed in Ada 2005. > I don't understand how to initialize limited types and their pointers. You can use default initialization. You can use a discriminant to pass information to the default. You can initialize nonlimited components of a limited object, as Dmitry showed. You can switch to nonlimited types. Or you can switch to Ada 2005. Here's an example with a discriminant: type Class (Initial_Value : Integer) is new Parent.Class with private; ... private type Class (Initial_Value : Integer) is new Parent.Class with record Value : Integer := Initial_Value; -- Or some expression -- like Func(Initial_Value)*2 -- would be legal, too. end record; Then you can say: ... := new Parent.Child.Class(Initial_Value => 5); and the heap object's Value component will be default-initialized to 5. > Any help? Again, my project currently is tied down to Ada95 (gnat > 3.16p) Sorry to hear that. 3.16p is quite old! - Bob
From: Jeffrey R. Carter on 2 Jan 2008 11:20 shaunpatterson(a)gmail.com wrote: > > package Parent is > > type Class is abstract tagged limited null record; > type Class_Access is access all Class'Class; > > end Parent; There's your problem. Don't use public access types. -- Jeff Carter "Run away! Run away!" Monty Python and the Holy Grail 58
From: Brian May on 2 Jan 2008 17:57 >>>>> "Jeffrey" == Jeffrey R Carter <spam.jrcarter.not(a)acm.nospam.org> writes: Jeffrey> shaunpatterson(a)gmail.com wrote: >> >> package Parent is >> >> type Class is abstract tagged limited null record; >> type Class_Access is access all Class'Class; >> >> end Parent; Jeffrey> There's your problem. Don't use public access types. Why not? -- Brian May <bam(a)snoopy.apana.org.au>
|
Next
|
Last
Pages: 1 2 3 Prev: M-I,5`Per secution - BBC h 2g2 online Next: [gnat] Where did pragma CPP_Destructor go? |