Re: Ada 95 constructors on limited types



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
.



Relevant Pages

  • Re: ANN: Deepend 3.1 available for Ada 2005 and Ada 2012
    ... Can now deallocate the default subpool, ... Corrected build documentation for ICC Ada 2005 compiler ... In the previous version, there were compile time warnings I had added, that stated that GNAT currently routes all allocations to the default subpool, even if a specific subpool had been specified for the allocator. ...
    (comp.lang.ada)
  • Re: Ada containers and custom allocators
    ... Is it possible to set up a custom allocator for use with any given Ada ... If you mean an allocator for the private data of the container (which ... storage pool passed would have to be completely general, ...
    (comp.lang.ada)
  • Re: GNAT - return by anonymous access
    ... use an allocator for such a type. ... allocator in your Ada 2005 code. ... Thank you for pointing out the 2012 construct; ... simply declaring the object previously and initializing it with the ...
    (comp.lang.ada)
  • Re: Allocators and exceptions
    ... smarter allocator is enough. ... In case of exception the already constructed components are ... can call it that) with Ada is that is clearly defines when that will happen. ... times bigger and possibly 10 times slower. ...
    (comp.lang.ada)
  • Re: abstract sub programs overriding
    ... > that this first idiom you describe is wrong for Ada, ... anything about the elements of the parent, ... > an object before any Initialize for it has completed. ... possible elaboration order which the Ada rules don't allow. ...
    (comp.lang.ada)