Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The normal way to define a predicate for sorting is as a functor.</p> <p>Make your class define the <code>operator()</code> to do the comparing, and you can just pass an instance of the class to <code>std::sort</code>.</p> <p>So really, all you need to do is to define the class like this instead:</p> <pre><code>class test { vector&lt;expr*&gt; _exprs; bool operator()(expr* e1, expr* e2); ExprManager* mg; } </code></pre> <p>And then you can call <code>sort</code> like this:</p> <pre><code>sort(_exprs-&gt;begin(), _exprs-&gt;end(), test()); </code></pre> <p>or, of course, use an existing instance of the <code>test</code> class, instead of constructing a new one. But you just pass in a class instance, and don't need to mention the member function at all.</p> <p>If the sorting takes place in another member function (it looks that way from your reference to <code>_exprs</code>), write</p> <pre><code>sort(_exprs-&gt;begin(), _exprs-&gt;end(), *this); </code></pre> <p>One thing to note is that <code>std::sort</code>, like most other standard library algorithms, copies the predicate object, and so your predicate class must be able to handle copying safely (which your classes should always do <em>anyway</em>)</p> <p>In short, the way to achieve this is to follow the "rule of three".</p> <p>If your class defines a destructor, copy constructor <em>or</em> assignment operator, then it should almost certainly define all three.</p> <p>If the compiler-generated copy constructor is used, it will simply copy your class' pointer members, so you will have two objects containing pointers to the same object.</p> <p>If the class has a destructor which calls <code>delete</code> on that pointer, then that ends up being done twice. Which is an error.</p> <p>If your class is intended to be copyable (and most of the standard library requires this), then you must define appropriate copy constructors and assignment operators to implement it safely (for example, copy the resource pointed to by the pointer, or use a smart pointer instead).</p> <p>IF your class is <em>not</em> intended to be copyable, then you should define the copy constructor and assignment operators to be <code>private</code>, so that attempts to copy the class will result in a compile-time error, instead of runtime crashes.</p> <p>You should <em>never ever</em> define a class which can be copied, but doesn't do so correctly. Either define the necessary copy constructor/assignment operator/destructors to <em>handle</em> copying, or make copying impossible by making copy ctor/assignment operator private.</p> <p>Wrapping pointers to dynamically allocated memory owned by the class in smart pointers is a simple way to get copyability for free.</p> <p>If the class contains only RAII objects, then you don't need to define a destructor at all, and so the rule of three doesn't require you to define copy constructor and assignment operator either.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload