comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen.a.leake.1@gsfc.nasa.gov>
Subject: Re: When to use 'Class in a parameter list
Date: 23 Jul 2001 18:55:09 -0400
Date: 2001-07-23T23:12:29+00:00	[thread overview]
Message-ID: <ubsmbz25u.fsf@gsfc.nasa.gov> (raw)
In-Reply-To: 9ji1b3$4pi$1@nh.pace.co.uk

"Marin David Condic" <marin.condic.auntie.spam@pacemicro.com> writes:

> O.K. Here's something I thought I understood but given behavior of some code
> I have, now I'm questioning what is happening.
> 
> <snip>
> 
> In code:
> 
> procedure Op (Base : in out Base_Type) ;
> ...

just for clarity:

type Child_Type is new Base_Type; 

> X : Child_Type ;
> ...
> Op (X) ; --  Why is this working without a Base_Type (X)
> conversion???

Because when you declare a derived type (Child_Type), you implicitly
declare all the 'primitive operations' of the parent type, for the
child type. So you now have:

procedure Op (Base : in out Child_Type);

This is the whole idea of derived types; it is important to understand
this feature.
 
> So unless I'm doing something strange that is causing some corner-case to
> come up, I'm now wondering why I would need Base_Type'Class as a parameter
> type? I was under the impression that I would use 'Class if I wanted to make
> an operation that worked on anything derived from the class without explicit
> conversion. 

Yes, this is true.

> (Possible to override it in a child class, AFAIK...)

I'm not clear what you mean by this. If you declare:

procedure Class_Wide_Op (Foo : in Base_Type'class);

then it is _not_ a 'primitive operation', since the type of the
argument is _not_ 'Base_Type' (Base_type'class is a _different_
type!). Only 'primitive operations' can be overridden in derived
types.

> My understanding of when to *NOT* use the 'Class was if I was building an
> operation I expected to override (possibly calling the parent operation
> within it - using a type conversion). So when it is not overriden, and
> control goes to the parent op without an explicit conversion, then when do
> you need the 'Class? I must be missing something here......(I need to do
> this sort of thing more often - it all evaporates if you don't use
> it!!!)

Hmm. When you do not explicitly override Op, the implicitly declared
operation gets an implicit body that is the same as the parent body.

Another difference between a 'Class parameter and a plain parameter;
when you call a primitive operation with a 'Class value, you get
run-time dynamic dispatching. When you call a primitive operation with
a plain value, you get compile-time static dispatching. I'm not clear
if this matters in your case.

-- 
-- Stephe



  parent reply	other threads:[~2001-07-23 22:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-23 20:27 When to use 'Class in a parameter list Marin David Condic
2001-07-23 21:39 ` Ehud Lamm
2001-07-24 12:49   ` Marin David Condic
2001-07-24 14:39     ` Dmitry A. Kazakov
2001-07-24 15:16     ` Ehud Lamm
2001-07-24 17:16       ` Marin David Condic
2001-07-23 22:55 ` Stephen Leake [this message]
2001-07-25 19:20   ` Deligation with Ada95 Hans-Olof Danielsson
2001-07-26  2:06     ` Lao Xiao Hai
2001-07-24  2:22 ` When to use 'Class in a parameter list Vincent Marciante
2001-07-24 12:52   ` Marin David Condic
2001-07-24 14:36     ` Ed Falis
2001-07-24 15:29       ` Ehud Lamm
2001-10-29 22:52 ` Matthew Heaney
replies disabled

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