From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,366b213c4abb1039 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!b15g2000yqd.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: What makes a procedure call 'dispatching' in Ada? Date: Fri, 20 Nov 2009 06:54:01 -0800 (PST) Organization: http://groups.google.com Message-ID: <879f8a3b-c2f8-4ea8-98c9-af5afae10b00@b15g2000yqd.googlegroups.com> References: NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1258728841 20440 127.0.0.1 (20 Nov 2009 14:54:01 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 20 Nov 2009 14:54:01 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b15g2000yqd.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:8177 Date: 2009-11-20T06:54:01-08:00 List-Id: 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.