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.9 required=5.0 tests=BAYES_00, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,901038687c38f61c X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!newsread.com!newsprint.newsread.com!63.218.45.10.MISMATCH!newshosting.com!nx01.iad01.newshosting.com!newsfeeds.sol.net!posts.news.twtelecom.net!nnrp3.twtelecom.net!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada References: Subject: Re: Idiom for a class and an object in Ada Date: Thu, 21 Oct 2004 18:02:32 -0400 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Message-ID: <417831f8$0$36214$39cecf19@news.twtelecom.net> Organization: Time-Warner Telecom NNTP-Posting-Date: 21 Oct 2004 22:02:32 GMT NNTP-Posting-Host: a6ff23e1.news.twtelecom.net X-Trace: DXC=;m]]\RPYVE0k9S[AF=UEb5C_A=>8kQj6==_1NR_H?JP=^:T@Q3dYZAA8S: "Kevin Cline" wrote in message news:e749549b.0410211131.6505da@posting.google.com... > > class AD_Converter > { > private: > > AD_Converter(int line) {} > public: > static AD_Converter Noise; > }; > > AD_Converter AD_Converter::Noise(17); Yes, indeed. I should have made this more clear. Actually, directly translating the C++ code above to Ada is a little tricky, since you can't declare object Noise until the full view of the type has been declared. That's what motivated my use of a selector-style function to return a (constant) reference to the object declared in the body (or in the private part of the spec). But the real issue is that a function returns a constant reference, so if you want to be able to use an operation to actually modify the object you need to do something else. One way is to use the Rosen Trick: package AD_Converts is type AD_Converter (<>) is limited private; procedure Op (ADC : AD_Converter); --modifier function Noise return AD_Converter; private type Handle (ADC : access AD_Converter) is limited null record; type AD_Converter is limited record H : Handle (ADC'Access); end record; end; package body AD_Converters is procedure Op (ADC : AD_Converter) is X : AD_Converter renames ADC.H.ADC.all; begin ... -- modify X as desired end; ... end AD_Converters; Part of the problem is that we were given a requirement that we can't use access types. If we didn't have that requirement, then you do this: package AD_Converter is type AD_Converter (<>) is limited private; procedure Modifier (ADC : access AD_Converter); procedure Selector (ADC : access constant AD_Converter); type ADC_Access is access all AD_Converter; for ADC_Access'Storage_Size use 0; Noise : constant ADC_Access; private type AD_Converter is limited record ... end record; --no more Rosen Trick (sorry, Jean-Pierre...) Noise_ADC : aliased AD_Conveter; Noise : constant ADC_Access := Noise_ADC'Access; end; However, all we're really doing is using the AD_Converter type as a fancy way to identify a statically declared object. We have simpler ways of doing that, especially if the "object" is just an array component: package AD_Converters is type AD_Converter (<>) is limited private; procedure Modifier (ADC : in out AD_Converter); procedure Selector (ADC : in AD_Converter); Noise : constant AD_Converter; private type AD_Converter is new Positive; Noise : constant AD_Converter := 1; end; package body AD_Converters is type Rep_Type is limited record ... end record; Objects : array (AC_Converter range 1 .. ) of Rep_Type; procedure Modify (ADC : in out AD_Converter) is Object : Rep_Type renames Objects (ADC); begin ... end; ... end;