Search code examples
c++encapsulationmergesort

How would I encapsulate a mergesort program for general customer use in C++?


I currently have a mergesort that accepts a list of ints and makes an array out of them, then sorts them, and prints out the sorted array. Currently all of the code is in a single .cpp file.

What is a good way to provide the code to someone who wants to sort an array of user defined objects?

My instincts are to provide a virtual method only file (interface) and require my user to override comparison operators and read/write methods.

Would it be best to move away from arrays and use a linked list?

If this is too vague/subjective then just slap me around and close it. I just wanted some ideas beyond my own.


Solution

  • Use templates to implement the sort and use a pointer array instead of an array of objects ask for a functor that implements the comparison and also provide a default functor which uses < operator to do the comparison.