comp.lang.ada
 help / color / mirror / Atom feed
* virtual functions in ada95
@ 2001-06-16 17:31 Thomas Nebel
  2001-06-16 19:08 ` David C. Hoos, Sr.
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Thomas Nebel @ 2001-06-16 17:31 UTC (permalink / raw)


Hi

How can i use virtual functions in Ada? I have a superclass and some classes
that are derived
from it:

type TNTGL_Custom_Node_Record is tagged null record;
type TNTGL_Node_Record is new TNTGL_Custom_Node_Record with private;
type TNTGL_Visual_Node_Record is new TNTGL_Node_Record with private;
type TNTGL_Sphere_Record is new TNTGL_Visual_Node_Record with private;

type TNTGL_Node is access all TNTGL_Node_Record'Class;
type TNTGL_Sphere is access all TNTGL_Sphere_Record'Class;
....
procedure Run (Node : access TNTGL_Node_Record'Class);
procedure Run (Node : access TNTGL_Group_Node_Record'Class);
procedure Run (Sphere : access TNTGL_Sphere_Record'Class);

Now i want to "run" the classes from one point transparently :

Node : TNTGL_Node;
...
Node := Scene.First_Node;
while Node /= null loop
    Run (Node);
    Node := Get_Next (Node);
end loop;

Independent of the real type of the Node variable, the Run function is
always the Run from
TNTGL_Node_Record.

How can i manage it, that the Run function for the real Node type is called
?

Thomas





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

* Re: virtual functions in ada95
  2001-06-16 17:31 virtual functions in ada95 Thomas Nebel
@ 2001-06-16 19:08 ` David C. Hoos, Sr.
  2001-06-16 19:43 ` tmoran
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: David C. Hoos, Sr. @ 2001-06-16 19:08 UTC (permalink / raw)


Declare abstract subprograms in the root type, then when that type is
extended,
the compiler forces you to declare the (concrete) implementation of each
abstract subprogram.

Then, when the abstract subprogram is invoked, the call dispatches to the
implementation according to the derived type with which it was called.

"Thomas Nebel" <thomas_nebel@gmx.de> wrote in message
news:9gg557$8sgga$1@ID-42131.news.dfncis.de...
> Hi
>
> How can i use virtual functions in Ada? I have a superclass and some
classes
> that are derived
> from it:
>
> type TNTGL_Custom_Node_Record is tagged null record;
> type TNTGL_Node_Record is new TNTGL_Custom_Node_Record with private;
> type TNTGL_Visual_Node_Record is new TNTGL_Node_Record with private;
> type TNTGL_Sphere_Record is new TNTGL_Visual_Node_Record with private;
>
> type TNTGL_Node is access all TNTGL_Node_Record'Class;
> type TNTGL_Sphere is access all TNTGL_Sphere_Record'Class;
> ....
> procedure Run (Node : access TNTGL_Node_Record'Class);
> procedure Run (Node : access TNTGL_Group_Node_Record'Class);
> procedure Run (Sphere : access TNTGL_Sphere_Record'Class);
>
> Now i want to "run" the classes from one point transparently :
>
> Node : TNTGL_Node;
> ...
> Node := Scene.First_Node;
> while Node /= null loop
>     Run (Node);
>     Node := Get_Next (Node);
> end loop;
>
> Independent of the real type of the Node variable, the Run function is
> always the Run from
> TNTGL_Node_Record.
>
> How can i manage it, that the Run function for the real Node type is
called
> ?
>
> Thomas
>
>



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



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

* Re: virtual functions in ada95
  2001-06-16 17:31 virtual functions in ada95 Thomas Nebel
  2001-06-16 19:08 ` David C. Hoos, Sr.
@ 2001-06-16 19:43 ` tmoran
  2001-06-18  3:24 ` Mark Lundquist
  2001-06-18 18:54 ` Thomas Nebel
  3 siblings, 0 replies; 5+ messages in thread
From: tmoran @ 2001-06-16 19:43 UTC (permalink / raw)


>How can i use virtual functions in Ada?
  "An Ada dispatching subprogram is analogous to a C++ virtual function."
p 27, Ada as a Second Language, ISBN 0-07-011607-5

> type TNTGL_Node_Record is new TNTGL_Custom_Node_Record with private;
> type TNTGL_Visual_Node_Record is new TNTGL_Node_Record with private;
> type TNTGL_Sphere_Record is new TNTGL_Visual_Node_Record with private;

> procedure Run (Node : access TNTGL_Node_Record'Class);
> procedure Run (Sphere : access TNTGL_Sphere_Record'Class);

  A TNTGL_Sphere_Record, or any descendant of one, is also a descendant of
TNTL_Node_Record.  Which Run would you like to call in that case?
I think you want to drop the 'Class from the Run procedure declarations,
and have Run dispatch based on the run-time type of its paramter.

  procedure Run (Node : access TNTGL_Node_Record);
  procedure Run (Sphere : access TNTGL_Sphere_Record);
anything from a TNTGL_Node_Record up to the parent of a
TNTGL_Sphere_Record would call the first Run, a Sphere or its
descendants would call the second Run.



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

* Re: virtual functions in ada95
  2001-06-16 17:31 virtual functions in ada95 Thomas Nebel
  2001-06-16 19:08 ` David C. Hoos, Sr.
  2001-06-16 19:43 ` tmoran
@ 2001-06-18  3:24 ` Mark Lundquist
  2001-06-18 18:54 ` Thomas Nebel
  3 siblings, 0 replies; 5+ messages in thread
From: Mark Lundquist @ 2001-06-18  3:24 UTC (permalink / raw)



"Thomas Nebel" <thomas_nebel@gmx.de> wrote in message
news:9gg557$8sgga$1@ID-42131.news.dfncis.de...
> Hi
>
> How can i use virtual functions in Ada? I have a superclass and some
classes
> that are derived
> from it:
>
> type TNTGL_Custom_Node_Record is tagged null record;
> type TNTGL_Node_Record is new TNTGL_Custom_Node_Record with private;
> type TNTGL_Visual_Node_Record is new TNTGL_Node_Record with private;
> type TNTGL_Sphere_Record is new TNTGL_Visual_Node_Record with private;
>
> type TNTGL_Node is access all TNTGL_Node_Record'Class;
> type TNTGL_Sphere is access all TNTGL_Sphere_Record'Class;
> ....
> procedure Run (Node : access TNTGL_Node_Record'Class);
> procedure Run (Node : access TNTGL_Group_Node_Record'Class);
> procedure Run (Sphere : access TNTGL_Sphere_Record'Class);

OK... don't declare the parameters to the Run subprograms to have classwide
types.  You want them to be primitive to these types.

A classwide parameters says "here is something for me, and also anything
derived from me", i.e. it applies across the derivation class.  (I'm
assuming you realize that Ada uses the word "class" to mean something
completely different than what it means to the rest of the OOPL world...).

Now, there _is_ a connection between classwide types and dispatching...
Specifically, dispatching oocurs only if you have an actual parameter of a
classwide type matched with a formal parameter that is of a specific type.

Also... what's the rationale for using access parameters to Run rather than
'in' or 'in out' parameters?  Just curious...

Hope this keps,
- mark






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

* Re: virtual functions in ada95
  2001-06-16 17:31 virtual functions in ada95 Thomas Nebel
                   ` (2 preceding siblings ...)
  2001-06-18  3:24 ` Mark Lundquist
@ 2001-06-18 18:54 ` Thomas Nebel
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Nebel @ 2001-06-18 18:54 UTC (permalink / raw)


Hi

Thank You, it works now so far.
Probably it has not been the last question like that...

Thomas





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

end of thread, other threads:[~2001-06-18 18:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-16 17:31 virtual functions in ada95 Thomas Nebel
2001-06-16 19:08 ` David C. Hoos, Sr.
2001-06-16 19:43 ` tmoran
2001-06-18  3:24 ` Mark Lundquist
2001-06-18 18:54 ` Thomas Nebel

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