comp.lang.ada
 help / color / mirror / Atom feed
From: Mark & Zurima McKinney <mckmark@us.net>
To: Boaz <boaz@pipeline.com>
Subject: Re: passing pointer of different type to one procedure
Date: 1997/04/18
Date: 1997-04-18T00:00:00+00:00	[thread overview]
Message-ID: <3357BA42.654F@us.net> (raw)
In-Reply-To: 01bc4a80$713ca380$52bd0c26@cat


Boaz wrote:
> 
> Hi there,
> 
> I am trying to write a sort procedure for a data base project.  I have 2
> different records.  Instead of having 2 instances for that sort procedure,
> I am thinking to use just one that take a pointer of either one of the
> records type.
> 
> this is my test program but I don't quite sure how to set it up :
> 
> with text_io; use text_io;
> with ada.float_text_io; use ada.float_text_io;
> 
> procedure test is
> 
> type record_pointer is access all record;  <-- error
> 
> type a is record
>         key : float;
>         d : integer;
> end record;
> 
> type b is record
>         key : float;
>         e : float;
> end record;
> 

--You can make then the same type with a discriminant record ...
--Link so ...

type record_types is (A, B);

type Keyed_Record(Record_Type : Record_Types) is 
   record
      Key : Float;
      case Record_Type is
         when A =>
            I : Integer;
         when B =>
            F : Float;
      end case;
   end record;    

--Now the ponter will bee of one type and can point to 2 or more
different types of 
--discriminant records.

type Keyed_Record_Pointer is access Keyed_Record;

--It is likely that you would want to sort an array particularly if
there 
--will be more than two items to sort.

type Keyed_Record_Pointer_Arrays is array(natural range <>) of
Keyed_Record_Pointer;

--this might also be useful
function "<"(Left, Right : in Keyed_Record) return Boolean is
begin
    return Left.Key < Right.Key;
end;

In Ada95 you can also use inheritance.

type keyed_class is tagged 
   record
      Key : Float; 
   end record;

type key_record_pointer is access all keyed_Class;

function "<"(Left, Right : Keyed_Class'Class) return boolean is ......


--perhaps in a child package
type integer_keyed_class is new keyed_class with 
   record
      I : integer;
   end record;

--perhaps in a another child package
type float_keyed_class is new keyed_class with 
   record
      F : float;
   end record;


Enough said this should point you in the right direction.

MArk McKinney




      parent reply	other threads:[~1997-04-18  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-04-16  0:00 passing pointer of different type to one procedure Boaz
     [not found] ` <33555ECA.44A8@bix.com>
1997-04-17  0:00   ` Robert Dewar
     [not found]   ` <01bc4ad3$f3702860$6aba0c26@cat>
1997-04-17  0:00     ` Tom Moran
1997-04-17  0:00 ` John Harbaugh
1997-04-19  0:00   ` Robert Dewar
1997-04-18  0:00 ` Mark & Zurima McKinney [this message]
replies disabled

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