Article View: borland.public.delphi.language.objectpascal
Article #32550Re: Function that returns a Class
From: Mark Shiffer (Team M)
Date: Fri, 22 Nov 2002 13:08
Date: Fri, 22 Nov 2002 13:08
61 lines
2540 bytes
2540 bytes
With the result of the function being a TObject, yes it is the same, its just poor programming practice. Notice he mentioned scope. You normally do not want to "hide" the creation of an object in such a manner, but there can be reasons for it at times. "BobRoyAce" <bob@omegasoftwareinc.com> wrote in message news:3ddda7b1$1@newsgroups.borland.com... > Wouldn't your solution just be the same as doing the following? > > var > MyObj: TObject; > begin > MyObj := SomeProc(1); > MyObj.DoSomething; > MyObj.Free; > ... > end; > > function SomeProc(AOption: Integer); > begin > Result := TObject.Create(AOption); > end; > > > "Ray Marron" <marron+delphi@cableaz.com> wrote in message > news:3ddd63d1$1@newsgroups.borland.com... > > Dana Dill <dana@danadill.com> wrote in message > > news:3ddd61dd$1@newsgroups.borland.com... > > > Hi: > > > > > > I have a function that returns a class. I would like the returned > object > > to > > > be a copy and not a pointer to the passed object. How can I do this? > > > > This is usually done with a var parameter rather than having the function > > return an object. This way, the caller is responsible for the scope of > the > > variable. > > > > For example: > > > > var > > MyObj: TObject; > > begin > > SomeProc(1, MyObj); > > MyObj.DoSomething; > > MyObj.Free; > > ... > > procedure SomeProc(AOption: Integer; var AnObj: TObject); > > begin > > AnObj := TObject.Create(AOption); > > end; > > > > -- > > Ray Marron > > > > > >
Technical Details
Message-ID:
<3dde2c43$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mailReferences:
<3ddd61dd$1@newsgroups.borland.com> <3ddd63d1$1@newsgroups.borland.com> <3ddda7b1$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109d30,a90ac751e87011b2 X-Google-Attributes: gid109d30,public X-Google-ArrivalTime: 2002-11-22 05:17:23 PST From: "Mark Shiffer \(Team M\)" <na@na.com> Newsgroups: borland.public.delphi.language.objectpascal References: <3ddd61dd$1@newsgroups.borland.com> <3ddd63d1$1@newsgroups.borland.com> <3ddda7b1$1@newsgroups.borland.com> Subject: Re: Function that returns a Class Date: Fri, 22 Nov 2002 07:08:20 -0600 Lines: 63 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 199.96.9.129 Message-ID: <3dde2c43$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1037970499 199.96.9.129 (22 Nov 2002 05:08:19 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail