Creating array in main and then calling a sorting function from sorting class.
1 2 3 4
const size_t SIZE = 100;
int *array = newint [SIZE];
//fill array with ints, not shown here
quickSort(array, SIZE); //calling the sorting function in the sorting class
In the sorting class, quickSort is declared as such:
void quickSort(int arr, int num);
Everything works great.
Version 2 (issues)
Instead of creating the array in main, I have set up a class for the array, MyArrayClass,
where I create an object containing the array of ints. So far so good. The issues come when I write a member function to call quickSort. Eventhough my MyArrayClass object contains an array of ints, the code calling for quickSort() won’t compile as the data type isn’t ints but MyArrayClass (which in turn holds ints though). The compiler (using VS 2013 btw) complains that quickSort can’t convert the first argument from ’const MyArrayClass’ to ’int’.
How do I cast my class object array of ints as an int in order to be able to call the quickSort function? Or should I solve this issue in some other way? I tried altering the sorting function to accept the object as it is, but that only created an avalanche of new errors, so thinking that converting/casting the object array --> int might be easier...
1. writing a getter wouldn't change the data type, my issue is not with accessing/passing the array to the sorting function as such (but rather the type).
2. I'd like to use my existing sorting class...
The problem is that MyArrayClass isn't a vector of ints. It's an object, one of whose members is an array of ints. You can't simply cast it to be a vector, because it isn't a vector. You've effectively hidden that vector of ints inside the class.
This is the whole point of data abstraction and encapsulation. You don't expose the implementation of the class - instead, you give your class an interface that defines how the other code can interact with it.
If you want the code that uses that class to be able to sort the vector, then options are:
1) As fxj said, provide an interface method giving access to that array of ints. You can add a getter method so that the calling code can obtain that vector, so that the calling code can use your existing sorting class to sort the vector. You can also add a setter method, so that the calling code can pass that sorted vector back to the object.
2) A much better approach would be to add a sorting method to MyArrayClass. That method can use your existing sorting class to sort the vector. This fits much better with the principle of data abstraction.
I did try using a getter, however this resulted in the same error when trying to pass it to the sorting class. Because I was still passing a MyArrayClass object, (i e the pointer to the memory address of the first item in the array), right? The way I figure I could make it work using getters/setters then, would be to create an int array, then copy the contents of the object array to the int array, then sort, then copy back. Correct?
Anyway, I did go with your and zsteve's second suggestion (adding the sorting method to MyArrayClass), and that worked out, had to overload the > and  operators of MyArrayClass though when adapting the sorting function.
Thanks again zsteve and MikeyBoy.
Leaving this thread open a bit more, just in case someone else has something to add...
If you're adding a method in MyArrayClass, then that method has direct access to the class's private data members. So you can pass the integer array member itself directly into your existing code that does the sort.