comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.Leake@gsfc.nasa.gov>
Subject: Re: Converting C++ class to Ada
Date: 1996/12/13
Date: 1996-12-13T00:00:00+00:00	[thread overview]
Message-ID: <32B18347.52AB@gsfc.nasa.gov> (raw)
In-Reply-To: 32AECEBE.2B5D@gsfc.nasa.gov


Since yesterday, I've talked to a C++ guru, and have some further
information :)

Stephen Leake wrote:
> 
> Ken Garlington wrote:
> >
> > In an attempt to better understand both C++ and Ada, I've been
> > converting some C++ code into Ada. I'm struggling with this particular
> > class:
> >
> >   class CTableXY
> >   {
> >   public:
> >     CMotor&   XMotor;
> >     CMotor&   YMotor;
> >     CSwitch&  XLimit;
> >     CSwitch&  YLimit;
> >     CTableXY( CMotor& xmotor, CMotor& ymotor,
> >        CSwitch& xlim, CSwitch& ylim ) :
> >        XMotor(xmotor), YMotor(ymotor),
> >        XLimit(xlim), YLimit(ylim) {}
> >     void SetVelocityX( float vel )
> >     { XMotor.SetSpeed( vel ); }
> >     void SetVelocityY( float vel )
> >     { YMotor.SetSpeed( vel ); }
> >     int GetSwitchX( void ) { return
> >       XLimit.isOpen(); }
> >     int GetSwitchY( void ) { return
> >       YLimit.isOpen(); }
> >   };
> >
> > It seems to me that the straightforward Ada translation is something
> > like:
> >
> >   with Motor, Switch;
> >   package Table_XY is
> >
> >     type Motor_Access_Type is access all Motor.Object_Type'Class;
> >     type Switch_Access_Type is access all Switch.Object_Type'Class;
> >
> >     type Object_Type is record
> >        X_Motor : Motor_Access_Type;
> >        Y_Motor : Motor_Access_Type;
> >        X_Limit : Switch_Access_Type;
> >        Y_Limit : Switch_Access_Type;
> >     end record;
> >
> >     procedure Set_Velocity_X ( Object : in out Object_Type;
> >                                Velocity : in Motor.Velocity_Type );
> >     -- and so forth
> >
> >   end Table_XY;
> 
> I'm not clear why a class would have reference members, but if I were
> building this type, the components of Object_Type would NOT be access
> types; I want the class to have control over when they change. I'll
> ignore this issue in the following.

A C++ reference is a constant (it always refers to the same object), so
using an access type in Ada definitely loses something (you can change
what object it points to). My C++ guru says he uses references as class
members mainly for temporary objects in windowing systems; binding a
widget and a gidget together into a whatsis, where the widget and gidget
already exist.

> 
> >
> > My main issues:
> >
> >   (1) What do I do about the constructor? As I understand C++,
> >       I need to force the user to supply values for the components of
> >       Object_Type when something of that type is declared. How do I do
> > this?
> 
> Ahah! the Initialized component could be a discriminant without
> defaults; then the user must provide a full aggregate when the object is
> declared:
> 
> type Object_Type (Initialized : BOOLEAN) is record
>    case Initialized is
>    when False =>
>       null;
>    when True =>
>      X_Motor : Motor_Access_Type;
>      Y_Motor : Motor_Access_Type;
>      X_Limit : Switch_Access_Type;
>      Y_Limit : Switch_Access_Type;
>    end case;
> end record;

Oops; this doesn't get there. It is perfectly legal to declare:

Foo : Object_Type (True);

and the fields exist without being initialized.


-- 
- Stephe




  reply	other threads:[~1996-12-13  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-12-10  0:00 Converting C++ class to Ada Ken Garlington
1996-12-11  0:00 ` Larry Kilgallen
1996-12-11  0:00 ` Stephen Leake
1996-12-13  0:00   ` Stephen Leake [this message]
1996-12-11  0:00 ` Jon S Anthony
1996-12-12  0:00   ` Ken Garlington
1996-12-18  0:00   ` Jon S Anthony
1996-12-18  0:00     ` Matthew Heaney
1996-12-19  0:00     ` Robert A Duff
1996-12-20  0:00       ` Stephen Leake
1996-12-20  0:00         ` Robert A Duff
1996-12-20  0:00   ` Norman H. Cohen
1996-12-20  0:00   ` Jon S Anthony
1996-12-20  0:00     ` Mitch Gart
1996-12-21  0:00   ` Jon S Anthony
1996-12-11  0:00 ` Norman H. Cohen
1996-12-12  0:00   ` Jon S Anthony
1996-12-12  0:00 ` Dale Stanbrough
1996-12-12  0:00   ` Constructors for Ada (was: Converting C++ class to Ada) Matthew Heaney
1996-12-13  0:00     ` Norman H. Cohen
1996-12-13  0:00       ` Matthew Heaney
1996-12-17  0:00   ` Robert I. Eachus
replies disabled

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