comp.lang.ada
 help / color / mirror / Atom feed
* Problem whith objects, access and abstract tagged private types
@ 2004-01-08 13:58 Bruno
  2004-01-08 14:56 ` Georg Bauhaus
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Bruno @ 2004-01-08 13:58 UTC (permalink / raw)


Hello to all,

I'm newbie in Ada95 and I have some difficulties with the language.
I'll give you my specifications :

Specif (.ads) :
---------------

   package Objets is

      type Item_Type is abstract tagged private;
      -- Action
      procedure Traitement (F : in Item_Type);
      -- Allows init
      procedure Modifier (F  : in out Item_Type'Class;
                          Nb : in     Natural);

      type Item_Elmt is private;
      function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt;
      function To_Item_Type (F : in Item_Elmt) return Item_Type'Class;

   private

      type Item_Type is abstract tagged
         record
            Compteur : Natural := 0;
         end record;
      type Item_Elmt is access all Item_Type'Class;

   end Objets;

I chose private types for hiding implementation of my types and access
types.

My problem is that I can't make the function "To_Item_Elmt" that create
"Item_Elmt".

I try to write this body :

   function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
      My_Tag : constant Ada.Tags.Tag := F'Tag;
   begin
      return new My_Tag' (F);
   end To_Item_Elmt;

The compilation indicates the following errors :
    subtype mark required in this context
    found "My_Tag" declared at line 30

If somebody could help and explain me how to resolve my problem, thanks for
his help.

Bruno






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

* RE: Problem whith objects, access and abstract tagged private types
@ 2004-01-08 14:51 amado.alves
  0 siblings, 0 replies; 5+ messages in thread
From: amado.alves @ 2004-01-08 14:51 UTC (permalink / raw)
  To: comp.lang.ada

<<
...
      type Item_Elmt is access all Item_Type'Class;
...
   function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
      My_Tag : constant Ada.Tags.Tag := F'Tag;
   begin
      return new My_Tag' (F);
   end To_Item_Elmt;
>>

According to your own definition of Item_Elmt and To_Item_Elmt you must return an access value, not a Tag. Maybe you want to return an access to F. As it stands, you cannot, because F might be a value. If the actual parameter passed is always a variable then use mode in out for F. The return something along the lines of F'Access.



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

* Re: Problem whith objects, access and abstract tagged private types
  2004-01-08 13:58 Problem whith objects, access and abstract tagged private types Bruno
@ 2004-01-08 14:56 ` Georg Bauhaus
  2004-01-08 15:22 ` Dmitry A. Kazakov
  2004-01-08 18:31 ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2004-01-08 14:56 UTC (permalink / raw)


Bruno <b.santo@free.fr> wrote:
:      type Item_Elmt is access all Item_Type'Class;
: 
:   function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
:      My_Tag : constant Ada.Tags.Tag := F'Tag;
:   begin
:      return new My_Tag' (F);
:   end To_Item_Elmt;
 
Do you want to return a pointer to F, or
do you want to return a pointer to a newly allocated object of
F's type?

Georg



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

* Re: Problem whith objects, access and abstract tagged private types
  2004-01-08 13:58 Problem whith objects, access and abstract tagged private types Bruno
  2004-01-08 14:56 ` Georg Bauhaus
@ 2004-01-08 15:22 ` Dmitry A. Kazakov
  2004-01-08 18:31 ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2004-01-08 15:22 UTC (permalink / raw)


On Thu, 8 Jan 2004 14:58:44 +0100, "Bruno" <b.santo@free.fr> wrote:

>Hello to all,
>
>I'm newbie in Ada95 and I have some difficulties with the language.
>I'll give you my specifications :
>
>Specif (.ads) :
>---------------
>
>   package Objets is
>
>      type Item_Type is abstract tagged private;
>      -- Action
>      procedure Traitement (F : in Item_Type);
>      -- Allows init
>      procedure Modifier (F  : in out Item_Type'Class;
>                          Nb : in     Natural);
>
>      type Item_Elmt is private;
>      function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt;
>      function To_Item_Type (F : in Item_Elmt) return Item_Type'Class;
>
>   private
>
>      type Item_Type is abstract tagged
>         record
>            Compteur : Natural := 0;
>         end record;
>      type Item_Elmt is access all Item_Type'Class;
>
>   end Objets;
>
>I chose private types for hiding implementation of my types and access
>types.
>
>My problem is that I can't make the function "To_Item_Elmt" that create
>"Item_Elmt".
>
>I try to write this body :
>
>   function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
>      My_Tag : constant Ada.Tags.Tag := F'Tag;
>   begin
>      return new My_Tag' (F);
>   end To_Item_Elmt;
>
>The compilation indicates the following errors :
>    subtype mark required in this context
>    found "My_Tag" declared at line 30
>
>If somebody could help and explain me how to resolve my problem, thanks for
>his help.

You cannot dispatch on bare tags.

1. Assuming that To_Item_Elmt indeed has to create a *copy* of the
argument, then it could be:

function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
begin
   return new Item_Type'Class'(F);
end To_Item_Elmt;

But it is a sort of suspicious design to hide the implementation of a
pointer type, not making the pointer "smart". Observe that the
following will leak:

declare
   X : Derived_From_Item_Type;
   Y : Item_Elmt := To_Item_Elmt (X);
begin
   null;
end; -- A dangling copy of X produced by To_Item_Elmt is still there

2. If To_Item_Elmt should not copy the argument but just get an access
to it, then it is impossible to do, because the argument is "in".
Although, to complete the picture, there is so-called Rosen trick to
circumvent that. [I would not recommed it for a novice] It goes as
follows:

   type Item_Type is abstract tagged limited private;
   ...
private
   type Item_Elmt is access all Item_Type'Class;
   type Item_Type is abstract tagged limited record
      Self : Item_Elmt := Item_Type'Unchecked_Access;
      Compteur : Natural := 0;
   end record;

function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
begin
   return F.Self;
end To_Item_Elmt;

3. However, probably, you need no Item_Elmt type at all, as a guess.

--
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



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

* Re: Problem whith objects, access and abstract tagged private types
  2004-01-08 13:58 Problem whith objects, access and abstract tagged private types Bruno
  2004-01-08 14:56 ` Georg Bauhaus
  2004-01-08 15:22 ` Dmitry A. Kazakov
@ 2004-01-08 18:31 ` Jeffrey Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2004-01-08 18:31 UTC (permalink / raw)


Bruno wrote:

> If somebody could help and explain me how to resolve my problem,
> thanks for his help.

Why are you using pointers at all? There seems to be no need for them.

-- 
Jeff Carter
"Apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, the fresh water system,
and public health, what have the Romans ever done for us?"
Monty Python's Life of Brian
80




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

end of thread, other threads:[~2004-01-08 18:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-08 13:58 Problem whith objects, access and abstract tagged private types Bruno
2004-01-08 14:56 ` Georg Bauhaus
2004-01-08 15:22 ` Dmitry A. Kazakov
2004-01-08 18:31 ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2004-01-08 14:51 amado.alves

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