comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: What makes a procedure call 'dispatching' in Ada?
Date: Fri, 20 Nov 2009 06:54:01 -0800 (PST)
Date: 2009-11-20T06:54:01-08:00	[thread overview]
Message-ID: <879f8a3b-c2f8-4ea8-98c9-af5afae10b00@b15g2000yqd.googlegroups.com> (raw)
In-Reply-To: he64pl$d8i$1@nntp.ilk.net

I'd like to follow up on the (correct) replies in this thread so far
with a "dynamic dispatching in Ada for C++ programmers" primer.

In C++:

class C {
   virtual void foo ();
}
C object;
C* pointer = &object;

void p () {
   object.foo (); // static dispatch
   pointer->foo (); // dynamic dispatch
}

In C++, class types are specific and pointer types are class-wide. The
declaration

C* pointer = &object;

is strictly equivalent to

Object : aliased C;
type C_Access is access all C'Class;
Pointer : C_Access := Object'Access;

Ada makes it explicit that Pointer is of a class-wide type, therefore
calls through the pointer dispatch dynamically:

procedure P is
begin
   Foo (Object); -- static dispatch
   Foo (Pointer.all); -- dynamic dispatch
end P;

So far it seems that Ada and C++ are really the same, but wait! Ada
has a syntax to declare access types to a specific type, like so:

type C_Specific_Access is access all C;

Any calls to primitive operations of C through C_Specific_Access will
dispatch statically, not dynamically. There is no way in C++ to
declare such a type. In C++, all pointer types are class-wide; this
applies also to the type of the implicit "this" parameter.

C onversely, C++ has no way to declare a class-wide type that is not a
pointer or reference type. Ada has C'Class for just this purpose. The
consequence is that, in Ada, you do not need any pointers to achieve
dynamic dispatching whereas C++ requires you to use pointers if you
want dynamic dispatching. Consider again:

void p () {
   object.foo (); // static dispatch
   pointer->foo (); // dynamic dispatch
}

There is no way to dispatch dynamically on "object"; you must use
"pointer"; contrast with Ada:

procedure P (Object : C) is
begin
   Foo (Object); -- static dispatch
   Foo (C'Class (Object)); -- safe dynamic dispatch, without pointers!
end P;

The construct C'Class (Object) is called a "view conversion" in Ada;
it entails no run-time cost and no additional object code in this case
(convert "up" the type hierarchy) but it allows the programmer to
choose whether each call should dispatch statically or dynamically.

HTH

--
Ludovic Brenta.



      parent reply	other threads:[~2009-11-20 14:54 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-20 13:15 What makes a procedure call 'dispatching' in Ada? Markus Schoepflin
2009-11-20  9:31 ` stefan-lucks
2009-11-20 14:10   ` Niklas Holsti
2009-11-20 13:27 ` Dmitry A. Kazakov
2009-11-20 13:43   ` Markus Schoepflin
2009-11-20 13:54     ` RasikaSrinivasan@gmail.com
2009-11-20 13:58     ` Markus Schoepflin
2009-11-20 14:19       ` Niklas Holsti
2009-11-21 14:07       ` Peter C. Chapin
2009-11-20 14:00     ` Niklas Holsti
2009-11-20 14:10     ` Dmitry A. Kazakov
2009-11-20 13:56 ` Niklas Holsti
2009-11-20 14:31   ` Dmitry A. Kazakov
2009-11-20 15:00     ` Niklas Holsti
2009-11-20 18:44       ` Dmitry A. Kazakov
2009-11-20 20:09         ` Niklas Holsti
2009-11-20 20:59           ` Dmitry A. Kazakov
2009-11-20 14:54 ` Ludovic Brenta [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