tombre@crin.crin.fr (Karl Tombre) (04/20/88)
Can somebody tell me how virtual procedures actually work in Simula-67? I am nearly sure that in Simula, as in C++, they are implemented by pointers to functions associated with each class. Thus, there is no need for a lookup function at runtime. Am I wrong? Thanks in advance for any answer (IT IS URGENT !!!), -- --- Karl Tombre @ CRIN / INRIA Lorraine EMAIL : tombre@crin.crin.fr - POST : BP 239, 54506 VANDOEUVRE CEDEX, France
ulfis@nada.kth.se (Anders Ulfheden) (04/26/88)
In article <450@crin.crin.fr> tombre@crin.crin.fr (Karl Tombre) writes: >Can somebody tell me how virtual procedures actually work in Simula-67? I am >nearly sure that in Simula, as in C++, they are implemented by pointers to >functions associated with each class. Thus, there is no need for a lookup >function at runtime. > >Am I wrong? In Simula, the VIRTUAL-specification of a procedure in a class and its subclasses makes objects contain only one copy of the procedure: that procedure that was defined in the deepest subclass when creating the object. Example: CLASS marin; VIRTUAL: INTEGER PROCEDURE foo; BEGIN INTEGER PROCEDURE foo; foo:=0; COMMENT top level foo ; REF (marin) next; ... END ** of CLASS marin **; marin CLASS submarin; BEGIN INTEGER PROCEDURE foo; foo:=17; COMMENT second level foo ; END ** of CLASS submarin **; submarin CLASS minisub; BEGIN INTEGER PROCEDURE foo; foo:=4711; COMMENT third level foo ; END ** of CLASS minisub **; REF (marin) pointer, tmp; INTEGER i; pointer:-NEW marin; tmp:-pointer; FOR i:=1 STEP 1 UNTIL 4 DO BEGIN IF Odd(i) THEN tmp.next:-NEW submarin ELSE tmp.next:-NEW minisub; tmp:-tmp.next; END; tmp:-pointer; WHILE tmp=/=NONE DO BEGIN Outint(tmp.foo, 5); tmp:-tmp.next; END; Outimage; Will result in: 0 17 4711 17 4711 End_of_example. ALL objects that are created from the class minisub, has the procedure foo that returns 1234; it doesn't exist any other procedures foo in any higher level above the class minisub. So independent from where in the classhierarchy you call foo, the result will be 4711. Exactly the same for objects created from the other classes submarin and marin; a call of the procedure foo will return 17 and 0 respectively. Note, that independent of the qualification of a reference pointing at an object, a virtual procedure is always reachable. No matter where is was defined. So, the answer to your question is yes. When CREATING an object, the virtual procdure gets redefined when a new version occurs. No lookup at runtime when accessing foo later. +------------------------------------------------------------------------------ | Anders Ulfheden | USENET: ulfis@nada.kth.se | Royal Institute of Technology | Stockholm, Sweden +------------------------------------------------------------------------------