Re: Question about OO programming in Ada

From: Ekkehard Morgenstern (ekkehard.morgenstern_at_onlinehome.de)
Date: 11/26/03


Date: Wed, 26 Nov 2003 01:22:06 +0100


"Martin Krischik" <krischik@users.sourceforge.net> schrieb im Newsbeitrag
news:21836716.smDW2dK3SM@linux1.krischik.com...
> Ekkehard Morgenstern wrote:
> > Do I have to use class-wide types for object-oriented programming, or
> > could I use regular access types?
> >
> > Like, when I declare a procedure
> >
> > procedure A ( B: in access all T'Class )
>
> I made this mistake as well when I started. For OO it should be only:
>
> procedure A ( B: in out T )
>
> Unlike C++ no access is needed in Ada. And a class wide Types make your
> procedure non "virtual". Some Tutorials are quite bad about describing
that
> point.

Are you sure?

Should it not read at least

    procedure A ( B: in out T'Class )

Because when I inherit from a class I would like the methods of the base
class to work on the derived class as well.

Both the Ada Rationale and the Guide for C/C++ programmers say that you have
to declare the first parameter of a procedure or the return value of a
function to be of the class-wide type of the class to get the dynamic
runtime dispatching behaviour from Ada (like virtual functions in C++).

How would I have to declare them?

And how do I cast an object back to the reduced record type? And is that
necessary at all?

I guess I'll have to write some simple test cases.

> > procedure A ( B: in access all T )
>
> Should be:
>
> procedure A ( B: access T )
>
> and is Ok.

The Ada 95 Reference says that the "all" attribute provides read-write
access to the object, while omitting it would only provide read-only access?
Or did I get that wrong?

(btw, I made short cuts with my examples, I know not all of the notation I
gave can be actually compiled)

> > procedure A ( B: in out T )
>
> Much better then using an acces type.

But how about inheritance? Will the procedure operate on a type derived from
T?

> > procedure A ( B: in T )
>
> Is like using "const&" in C++.

How about function returns? Can I return a reference to an object in the
same way?

> In Ada access type are not as often needed as in C++. So don't use then
> unless you need them. References are done automatily by Ada.
>
> If you know C++ then you will know about:
>
> virtual void A (T& B); or virtual void A (T B);
>
> As I said Ada will make the '&' Reference automaticly.

Does that help to avoid casting as well? And how do I return a reference
from a function? Or should I use procedures with in out parameters instead?

> The "all" part is needed access to an object which have not been created
> with "new".

You mean it doesn't give access to an object that was elaborated normally?

Now that explains some things.

> Using "all" for an access might reduce performace since the
> compiler might have to do some extra checking on the access. Avoid if not
> needed.

Ok.

> > I would like to program as cleanly as possible in Ada right from the
> > start, so I'd be glad if someone could give me some hints. :-)
>
> Hope I was of help.

Yes, thank you. :-)

> You might also browse my Web page http://adacl.sourceforge.net. There are
> lots of sources you can browse right on the web without downloading them.

Thanks, I have taken a look at it (not finished looking yet! ;-) ).



Relevant Pages

  • Ada 95 and Class attribute for none-tagged incomplete type
    ... This was allowed in Ada 95 even though we had not declared T as tagged ... incomplete if we wish to declare a class wide access type. ... So why to allow reference to a Class attribute in such circumstances? ...
    (comp.lang.ada)
  • Re: New to Perl: Need help with a script
    ... It might take some getting used to (you'll need to declare your variables, ... >sub usage() { ... You might want to use a here-doc (perldoc perldata) instead, ... pointing to the same array reference. ...
    (comp.lang.perl.misc)
  • Re: Excel Automation
    ... Declare a conditional compilation constant at the start of your module: ... #If EarlyBinding Then ... Dim xlApp As Excel.Application ... When you are ready to distribute the app, remove the reference, change the ...
    (microsoft.public.access.formscoding)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: A novel design of linked lists
    ... Ada does not provide them. ... elements of a double-linked lists as OK, then we are on different pages. ... I don't see why it wouldn't be OK to declare ... this definitely applies to the dopes of String objects. ...
    (comp.lang.ada)