Re: Ada 95 constructors on limited types
- From: Robert A Duff <bobduff@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 02 Jan 2008 11:02:03 -0500
shaunpatterson@xxxxxxxxx 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
.
- References:
- Ada 95 constructors on limited types
- From: shaunpatterson
- Ada 95 constructors on limited types
- Prev by Date: Re: Ada 95 constructors on limited types
- Next by Date: Re: Ada 95 constructors on limited types
- Previous by thread: Re: Ada 95 constructors on limited types
- Next by thread: Re: Ada 95 constructors on limited types
- Index(es):
Relevant Pages
|