From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,2f84446733b06aca X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,2f84446733b06aca X-Google-Attributes: gid103376,public From: jsa@alexandria (Jon S Anthony) Subject: Re: Converting C++ class to Ada Date: 1996/12/11 Message-ID: #1/1 X-Deja-AN: 203566026 sender: news@organon.com (news) references: <32ADF183.7195@lmtas.lmco.com> organization: Organon Motives, Inc. newsgroups: comp.lang.ada,comp.lang.c++ Date: 1996-12-11T00:00:00+00:00 List-Id: In article <32ADF183.7195@lmtas.lmco.com> Ken Garlington writes: > 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; > > 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? Two ways: 1. type Object_Type ( X_Motor : Motor_Access_Type; Y_Motor : Motor_Access_Type; X_Limit : Switch_Access_Type; Y_Limit : Switch_Access_Type ) is null record; (or better: is private) Now, whenever an object of this type is declared, you will have to supply the discriminants and there you are. Note that this works for the case of _limited_ types too. 2. type Object_Type (<>) is private; function Make ( X_Motor : Motor_Access_Type; Y_Motor : Motor_Access_Type; X_Limit : Switch_Access_Type; Y_Limit : Switch_Access_Type ) return Object_Type; ... private type Object_Type is record ... as you have it OR type Object_Type (... as in 1. Now, whenever an object of this type is declared you will have to initialize it. The only way you give is via Make, so you can force the supply of arguments. This way is especially useful with instances of limited types via allocators as you can have your own pool and know all clients must use that pool. > (2) If I'm reading it correctly, the C++ interface exposes the details > of the table, although it provides procedures to manipulate it. > However, > I'm thinking of making Object_Type limited private anyway, since > otherwise it seems silly to have the procedures/functions that are > declared here. (More of a style issue than anything...) Right. Except I don't see this as simply a "style" issue. It is more important than that. The C++ hack..ah..coder goofed and should have made the motor and switch stuff private (or protected). /Jon -- Jon Anthony Organon Motives, Inc. Belmont, MA 02178 617.484.3383 jsa@organon.com