comp.lang.ada
 help / color / mirror / Atom feed
* Ada95 OOP programming: calling correct superclass procedure
@ 1996-11-25  0:00 Gregory Lampshire
  1996-11-27  0:00 ` Jon S Anthony
  0 siblings, 1 reply; 2+ messages in thread
From: Gregory Lampshire @ 1996-11-25  0:00 UTC (permalink / raw)



I am having difficulty getting the right procedure to be called during
a dispatching operation:

package My_Objs is
	type A_Rec is tagged private;
	type A is access all A_Rec'Class;
	procedure Init (This : access A_Rec; I : Integer);
	function Create_A return A;

	type B_Rec is new A_Rec with private;
	type B is access all B_Rec'Class;
	procedure Init (This : access B_Rec; I : Integer);
	function Create_B return B;
end;
package body My_Objs is
	procedure Init (This : access A_Rec; I : Integer) is
	begin
		Do_Something_A_Specific (I);
	end;
	function Create_A return A is
		This : A := new A_Rec;
	begin
		Init (This);
		return This;
	end;


	procedure Init (This : access B_Rec; I : Integer) is
	begin
*		Init (This, I);
		Do_Something_B_Specific (I);
	end;
	function Create_B return B is
		This : B := new B_Rec;
	begin
		Init (This);
		return This;
	end;
end;

The "*" indicates where I have a problem.  If I do the following;

	declare
		Me : B := Create_B;
	begin
		...
	end;


I get stuck in an endless loop (of course) because Init is 
dispatching.  Note that both defs are in the same package so
I cannot prefix the call with the package name.  Also,
I want the user only to see the Create definition, not the 
Init but I had to stick into the package to force it to
be a primitive operation (for some definitions I do not
mind exposing Init).

I've tried casting using A'(This) but, of course, the tag
does the correct dispatching anyway.

How do I cast or resolve this problem within the constraints
above? (same package, same calling args except 1st parameter, 
same procedure name, reference argument controlling formal parameter)
I can always change the Init name to Init_A and Init_B but
I was wondering how to do this in Ada95.  It would be nice
to have something like Parent.Init (This) where Parent
is already defined as A in the above case.  Perhaps I have an
an OOP conceptual problem.




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

* Re: Ada95 OOP programming: calling correct superclass procedure
  1996-11-25  0:00 Ada95 OOP programming: calling correct superclass procedure Gregory Lampshire
@ 1996-11-27  0:00 ` Jon S Anthony
  0 siblings, 0 replies; 2+ messages in thread
From: Jon S Anthony @ 1996-11-27  0:00 UTC (permalink / raw)



In article <3299F04C.E86@mrj.com> Gregory Lampshire <gbol@mrj.com> writes:

> package My_Objs is
> 	type A_Rec is tagged private;
> 	type A is access all A_Rec'Class;
> 	procedure Init (This : access A_Rec; I : Integer);
> 	function Create_A return A;
> 
> 	type B_Rec is new A_Rec with private;
> 	type B is access all B_Rec'Class;
> 	procedure Init (This : access B_Rec; I : Integer);
> 	function Create_B return B;
> end;
...
> 	procedure Init (This : access B_Rec; I : Integer) is
> 	begin
> *		Init (This, I);
> 		Do_Something_B_Specific (I);
> 	end;
...
> The "*" indicates where I have a problem.  If I do the following;
...
> I get stuck in an endless loop (of course) because Init is 

Because you are call B's init inside B's init.  On the "*" line you
pass This to Init.  This is of type access to B_Rec.  Not surprisingly,
Init(access B_Rec,...) gets called again.

Change this to:

    Init(A(This), I)


> I've tried casting using A'(This) but, of course, the tag
> does the correct dispatching anyway.

Ooops!  Drop the "'"


/Jon
-- 
Jon Anthony
Organon Motives, Inc.
Belmont, MA 02178
617.484.3383
jsa@organon.com





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

end of thread, other threads:[~1996-11-27  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-11-25  0:00 Ada95 OOP programming: calling correct superclass procedure Gregory Lampshire
1996-11-27  0:00 ` Jon S Anthony

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