[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: ? about C++



begin  Dimitri Maziuk  quotation:

> Does anyone know about a school that teaches people to use
> vector<>, auto_ptr<>, basic_string and references?

I have no idea what they teach in school these days, but I should think
they would have to teach references if they teach operator overloading.

I'm actually a little surprised how rarely I use vector<> -- more often
I use deque<> when I want an array-like container, because the ability
to add and remove members at both ends comes in handy. Maybe that just
says something about the kind of data I work with.

The standard auto_ptr<> is an abomination. When more than one such
object points to the same data, only the most recently-assigned one
"owns" the pointer, which can lead to nasty, counter-intuitive problems
if you delete the auto_ptr<>s in a different order than you assigned
them. A few years back, I wrote my own AutoPtr<> that improves on
auto_ptr<> in at least three different ways:

(1) The data is only deleted when there are no more AutoPtrs referencing
    it;

(2) Deletion of the referenced data is handled by a function object
    (which is one of the template parameters), and therefore can do
    things other than just "delete ptr;" if needed (e.g. "delete[]
    ptr;", "ptr->Destroy();", or whatever);

(3) AutoPtr<> comes in two flavors, AutoPtr<> and ConstAutoPtr<>
    (read-only pointer), mimicking quite closely the relationship
    between X* and const X*, including the fact that you can assign an
    AutoPtr<X> to a ConstAutoPtr<X>, but not the other way around.

With this, you can safely put AutoPtr<>s to the same data in different
objects, and the data will stick around until it's no longer needed,
regardless of the order in which the objects are deleted.

Unfortunately, this code is not open-source.

Craig

Attachment: pgpSJtcbHOrsG.pgp
Description: PGP signature


Reply to: