comp.lang.ada
 help / color / mirror / Atom feed
* problems using classwide types
@ 1997-06-25  0:00 andy billimore
  1997-06-25  0:00 ` Matthew Heaney
  1997-07-01  0:00 ` John English
  0 siblings, 2 replies; 3+ messages in thread
From: andy billimore @ 1997-06-25  0:00 UTC (permalink / raw)



I'm looking for some help with classwide types.

I have defined a parent type -
  type Transaction_Type is tagged private;

And a classwide access type to objects of Transaction_Type'Class -
  type Transaction_Ptr_Type is access all Transaction_Type'Class;

I have then defined a child class, Transactions.Checks -
  type Check_Type is new Transaction_Type with private;

The main program looks like this -

with Transactions; use Transactions;
with Transactions.Checks; use Transactions.Checks;

procedure Main is
   L_Check : Check_Type;
   TP : Transaction_Ptr_Type;
begin
   TP := new Check_Type;
   TP := L_Check; -- Error (see below)
end Main;

Compiling this produces the following output - 

main.adb:24:10: expected type "Transaction_Ptr_Type" 
	        defined at transactions.ads:5

main.adb:24:10: found private type "Check_Type" 
		defined at transactions-checks.ads:2

gnatmake: "main.adb" compilation error

So I can allocate enough memory to hold an object of Check_Type, but I
can't assign anything to it which I don't understand.


Andrew




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

* Re: problems using classwide types
  1997-06-25  0:00 problems using classwide types andy billimore
@ 1997-06-25  0:00 ` Matthew Heaney
  1997-07-01  0:00 ` John English
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1997-06-25  0:00 UTC (permalink / raw)



In article <33B10EF3.72B4@dcs.gla.NOSPAM.ac.uk>, andy billimore
<billimad@dcs.gla.NOSPAM.ac.uk> wrote:


>procedure Main is
>   L_Check : Check_Type;
>   TP : Transaction_Ptr_Type;
>begin
>   TP := new Check_Type;
>   TP := L_Check; -- Error (see below)
>end Main;

>So I can allocate enough memory to hold an object of Check_Type, but I
>can't assign anything to it which I don't understand.

You don't have a "classwide types" problem, you have an "access types" problem.

The object TP is an access object.  It points to an object of type
Check_Type, but it not itself a Check_Type.

So when you do the assignment 

TP := L_Check;

you are trying to assign an apple (Check_Type) to an orange (a pointer to
Check_Type).

Just do this

TP.all := L_Check;

and you should be fine.

Of course, you can combine steps:

TP : Transaction_Pointer_Type := new Check_Type'(L_Check);

which does heap allocation and initialization ("assignment") all in one
step.  We wouldn't want to create any uninitialized objects, now would we,
hmmmm?

Hope that helps,
Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: problems using classwide types
  1997-06-25  0:00 problems using classwide types andy billimore
  1997-06-25  0:00 ` Matthew Heaney
@ 1997-07-01  0:00 ` John English
  1 sibling, 0 replies; 3+ messages in thread
From: John English @ 1997-07-01  0:00 UTC (permalink / raw)



andy billimore (billimad@dcs.gla.NOSPAM.ac.uk) wrote:
:   type Transaction_Type is tagged private;
:   type Transaction_Ptr_Type is access all Transaction_Type'Class;
:   type Check_Type is new Transaction_Type with private;

: The main program looks like this -

: with Transactions; use Transactions;
: with Transactions.Checks; use Transactions.Checks;
: procedure Main is
:    L_Check : Check_Type;

L_Check : aliased Check_Type; -- so you can get an access value to it

:    TP : Transaction_Ptr_Type;
: begin
:    TP := new Check_Type;
:    TP := L_Check; -- Error (see below)

-- the value assigned to TP must be of type Transaction_Ptr_Type
-- L_Check is a Check_Type, not a Transaction_Ptr_Type, so do this:
TP := L_Check'Access;  -- i.e. a "pointer" to L_Check

: end Main;

---------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.comp.it.bton.ac.uk/je
 Dept. of Computing        | fax: (+44) 1273 642405
 University of Brighton    |
---------------------------------------------------------------




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

end of thread, other threads:[~1997-07-01  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-25  0:00 problems using classwide types andy billimore
1997-06-25  0:00 ` Matthew Heaney
1997-07-01  0:00 ` John English

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