comp.lang.ada
 help / color / mirror / Atom feed
* virtual C++ functions in Ada?
@ 1998-10-26  0:00 Rolf Wester
  1998-10-28  0:00 ` Stephen Leake
  0 siblings, 1 reply; 2+ messages in thread
From: Rolf Wester @ 1998-10-26  0:00 UTC (permalink / raw)


Hi,

I tried to write a small Ada-program similar to a C++ programm which
uses virtual functions. But it does not work. The C++ prgramm is:

#include <iostream.h>
class beam
{
 public:
 double x,z,xs,zs;
 beam(double xx, double zz, double xxs, double zzs)
 {
  x = xx; z=zz; xs = xxs; zs = zzs;
 }
};
class OptElem
{
 public:
 OptElem() {}
 virtual void transform(beam & b) {}
};
class Propagator : public OptElem
{
 private:
 double L;
 public:
 Propagator(double LL)
 {
  L = LL;
 }
 virtual void transform(beam & b)
 {
  cout << "Propagator" << endl;
  //some code
 }
};
class Mirror : public OptElem
{
 private:
 double R;
 public:
 Mirror(double RR)
 {
  R = RR;
 }
 virtual void transform(beam & b)
 {
  cout << "Mirror" << endl;
  //some code
 }
};

main()
{
 beam b(0.0,0.0,0.0,1.0);

 OptElem * OE1 = new Propagator(10.0);
 OptElem * OE2 = new Mirror(20.0);
 Mirror  * M2  = new Mirror(30.0);

 OE1->transform(b);
 OE2->transform(b);

 return 0;
}

---------------------------------------

and the corresponding Ada prgram is:

package optelements is

   subtype RichtungsCosinus is Float range -1.0..1.0;

   type beam is
      record
         x  : Float;
         z  : Float;
         xs : RichtungsCosinus;
         zs : RichtungsCosinus;
      end record;

   type OptElem is tagged
      record
         dummy : Float;
      end record;
   type OptElem_Access is access all OptElem;
   type OptElemClass_Access is access all OptElem'Class;
   procedure transform(OE : in OptElem; b : in out beam);

   type Propagator is new OptElem with
      record
         L : Float;
      end record;
   type Propagator_Access is access all Propagator;
   function Propagator_Constructor(ll : in Float) return
Propagator_Access;
   procedure transform(OE : in Propagator; b : in out beam);
   -- this works

   type Mirror is new OptElem with
      record
         R : Float;
         D : Float;
      end record;
   type Mirror_Access is access all Mirror;
   function  Mirror_Constructor(rr : in Float; dd : in FLoat) return

Mirror_Access;
   procedure transform(OE : access Mirror; b : in out beam);
   -- this does not work

end optelements;

-----

with optelements; use optelements;

procedure optmain is
   b    : beam;
   OE1  : OptElemClass_Access;
   OE2 : OptElemClass_Access;
begin
   b.x  := 0.0;
   b.z  := 0.0;
   b.xs := 0.0;
   b.zs := 0.0;

   OE1  := OptElemClass_Access(Propagator_Constructor(20.0));
   OE2  := OptElemClass_Access(Mirror_Constructor(5.0, 3.0));

   transform(OE1.all, b);
   -- this works

   transform(OE1, b);
   -- this does'nt work

end optmain;

In the Ada program I get dynamic dispatching only when using objects for
the function parameters not when using access types. Is there anything
wrong?

I would be very appreciative for any help.

Thanks in anticipation

Rolf

wester@ilt.fhg.de





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

* Re: virtual C++ functions in Ada?
  1998-10-26  0:00 virtual C++ functions in Ada? Rolf Wester
@ 1998-10-28  0:00 ` Stephen Leake
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Leake @ 1998-10-28  0:00 UTC (permalink / raw)


Rolf Wester <WESTER@ilt.fhg.de> writes:

> Hi,
> 
> I tried to write a small Ada-program similar to a C++ programm which
> uses virtual functions. But it does not work. The C++ prgramm is:
>
<snip>

> package optelements is
>    ...
>    procedure transform(OE : in OptElem; b : in out beam);
>    ...
>    procedure transform(OE : in Propagator; b : in out beam);
>    -- this works
>    ...
>    procedure transform(OE : access Mirror; b : in out beam);
>    -- this does not work
> 
> end optelements;

> In the Ada program I get dynamic dispatching only when using objects for
> the function parameters not when using access types. Is there anything
> wrong?

'access Mirror' is not the same as 'Mirror'; the parameter type
profile for 'transform' on Mirror does not match that for 'transform'
on OptElem. Do get dynamic dispatching on an access value, you must
do:

package optelements is
   ...
   procedure transform(OE : access OptElem; b : in out beam);
   ...
   procedure transform(OE : access Propagator; b : in out beam);
   ...
   procedure transform(OE : access Mirror; b : in out beam);

end optelements;

all 'access', or all 'in' (or two variants of 'transform' for each
class).

Why did you want 'transform' on Mirror to be different?

-- Stephe




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

end of thread, other threads:[~1998-10-28  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-26  0:00 virtual C++ functions in Ada? Rolf Wester
1998-10-28  0:00 ` Stephen Leake

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