[LC++]STL and auto_ptr woes

George T. Talbot george at phat.com
Thu Mar 7 01:39:05 UTC 2002


I don't think that auto_ptr<> supports the assignment operator--it only
supports initialization via the constructor.  That's why it won't work in
your scenario.

Change that line to read like this:

::std::auto_ptr<someobject1> vb(BigMap2["located"].second.first);

That should get rid of the compile error.  However, this will fail horribly.
When vb goes out of scope, the object will be freed.  Then you'll have a
dangling pointer in your list and your program will crash later when that
item in the list is accessed or freed.

Is auto_ptr<> supposed to work in a Standard C++ Library collection?  I seem
to recall this as being a no-no, but I don't have the book in front of me
right now.

--George

P.S.  The list seems to be operating pretty slow.  I just received this at
about 9:35am EST on the 6th, and it was sent on the 4th and 2:45pm.



----- Original Message -----
From: "Charles Randle" <charlesrandle at hotmail.com>
To: <tuxcpprogramming at lists.linux.org.au>
Sent: Monday, March 04, 2002 2:45 PM
Subject: [LC++]STL and auto_ptr woes


> Hi All ,
>
> I am using the STL to store pointers to objects which are expensive to
copy
> (hence the pointers.
>
> I know it isn't a good idea to use the auto_ptr class with STL classes but
I
> don't readily remember why could anyone tell me why the following wouldn't
> be a good idea ?
>
> ::std::map <
> ::std::string,::std::pair<auto_ptr<someobject1>,auto_ptr<someobject2> > >
> bigMap;
>
> Given that the above isn't a good idea I have the following:
>
> ::std::map < ::std::string , ::std::pair<someobject1 *,someobject2 *> >
> BigMap2
>
> However when I assign the pointers to an auto_ptr object as in:
>
> ::std::auto_ptr<someobject1> vb = BigMap2["located"].second.first;
>
> the compiler gives the following error:
>
> simple.cpp:52:conversion from 'someobject1*' to non-scalar type
> 'std::auto_ptr<someobject1>' requested
>
>
> What is the reason for this error and what can I do to fix it ???
>
>
> Best Regards,
> Charles Randle
>
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
>





More information about the tuxCPProgramming mailing list