I have a vector<classType> vect
in order to call sort( vect.begin(), vect.end() ), what needs implemented in the class classType?
I have the overloaded operator< and a copy constructor, but the problem i get is that some member variables of classType that are pointers dereferenciate during the call to the sort function.

thx in advance

10 Years
Discussion Span
Last Post by Duoas

Do you mean you have something like this:

class Foo
    Baz* baz;  // <-- This gets shared between instances.
               //     It will cause problems... (See the destructor!)
    Foo(): baz( new Baz ) { }
    Foo( const Foo& foo ): baz( foo.baz ) { }
    ~Foo() { delete baz; }

And when you try to sort your program crashes?

Standard containers are notorious for copying stuff, even if you don't think you are doing something that would cause it to happen. (Of course, it is in their specification that they are allowed to do this.)

Likewise, the standard algorithms will crash and burn with a class like the one above, since you are almost guaranteed for copies to be created.

The best thing to do is either a deep copy or implement some form of reference counter. If you Google that, stay away from auto_ptr. It cannot be safely used with any STL container.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.