Article View: borland.public.cppbuilder.nativeapi
Article #13879Re: std::auto_ptr<> Help Please, Part 2
From: David
Date: Sun, 14 Sep 2003 15:51
Date: Sun, 14 Sep 2003 15:51
40 lines
2297 bytes
2297 bytes
dcent wrote:
> Thanks again to all for the help. I have another question about that
> nasty auto_ptr<> though. How can I pass what the auto_ptr is holding
> into a function?
>
> MyFile.h
> ---------
> private:
> void _fastcall LoadList(TStringList *sl); //or should it be
> (TStringList &sl)
>
> MyFile.cpp
> ---------
> _fastcall TForm::Button1Click() {
> std::auto_ptr<TStringList> sl(new TStringList(this));
> LoadList(sl); //<-- throws an error
> }
>
> The error is something like:
> Cannot convert auto_ptr<TStringList> to TStringList *
>
> Anyone got an idea?
>
> Thanks dcent
>
You can pass a reference, but it is only advisable when you KNOW that the
pointer will not contain NULL. As your class is guaranteed to contain a
real object by the auto_ptr design you could probably get away with it.
But if LoadList becomes a helper routine for other classes keep it as a
pointer and check for NULL.
.h:
void __fastcall LoadList(TStringList & s1);
.cpp:
__fastcall TForm1::Button1Click(TObject *Sender) {
std::auto_ptr<TStringList> s1(new TStringList);
LoadList(*s1.get()); //Note the "*"
}
Technical Details
Message-ID:
<3F648E90.4070403@anonymous_one.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!nntp5.savvis.net!newsfeed.frii.net!newsfeed.frii.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3F5E64B4.4050201@NetScape.net> <3F63E3D7.7050506@NetScape.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,da48c8ace4f19025 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-09-14 08:51:09 PST Message-ID: <3F648E90.4070403@anonymous_one.com> Date: Sun, 14 Sep 2003 10:51:44 -0500 From: David <david@anonymous_one.com> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: std::auto_ptr<> Help Please, Part 2 References: <3F5E64B4.4050201@NetScape.net> <3F63E3D7.7050506@NetScape.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 67.30.1.42 X-Trace: newsgroups.borland.com 1063554564 67.30.1.42 (14 Sep 2003 08:49:24 -0700) Lines: 41 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!nntp5.savvis.net!newsfeed.frii.net!newsfeed.frii.net!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail