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

Do you mean you have something like this:

class Foo
  {
  private:
    Baz* baz;  // <-- This gets shared between instances.
               //     It will cause problems... (See the destructor!)
  public:
    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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.