From: Rolf Wester <WESTER@ilt.fhg.de>
Subject: virtual C++ functions in Ada?
Date: 1998/10/26
Date: 1998-10-26T00:00:00+00:00 [thread overview]
Message-ID: <36343E19.624E8BB0@ilt.fhg.de> (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
next reply other threads:[~1998-10-26 0:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-10-26 0:00 Rolf Wester [this message]
1998-10-28 0:00 ` virtual C++ functions in Ada? Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox