How to pass a method to qsort?
<p>There is a class that contains some data and it sorts them at some point of time. I use <code>qsort()</code> and I'd like to keep the comparing function within the class as a method. The question is how to pass a method to <code>qsort()</code> so that the compiler (g++) don't throw any warnings?</p>

<p>Attempt 1:</p>

<pre><code>int Data::compare_records(void * rec_1, void * rec_2){
  // [...]
}

void Data::sort(){
  qsort(records, count, sizeof(*records), &amp;Data::compare_records);
}
</code></pre>

<p>This way generates an error:</p>

<pre><code>error: cannot convert ‘int (Data::*)(const void*, const void*)’ to ‘int (*)(const void*, const void*)’ for argument ‘4’ to ‘void qsort(void*, size_t, size_t, int (*)(const void*, const void*))’
</code></pre>

<p>Attempt 2 :</p>

<pre><code>void Data::sort(){
  qsort(
    records, count, sizeof(*records),
    (int (*)(const void*, const void*)) &amp;Data::compare_records
  );
}
</code></pre>

<p>This way generates a warning:</p>

<pre><code>warning: converting from ‘int (Data::*)(const void*, const void*)’ to ‘int (*)(const void*, const void*)’
</code></pre>

<p>How to do it the right way then?</p>
