Converting from class array to int[]

Version 1 (works fine)

Creating array in main and then calling a sorting function from sorting class.

In main:
1
2
3
4
const size_t SIZE = 100;
int *array = new int [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...
try adding a method called getIntArray() which returns a pointer to the int array or write an iterator class and use sort() in <algorithm>
Thanks zsteve, but

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...

Any other suggestions?
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.

Thanks MikeyBoy.

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...
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <algorithm>

struct myarray
{
    myarray(unsigned size) : _size(size), _array(new int[_size]()) {}

    unsigned size() const { return _size; }
    int& operator[](unsigned index) { return _array[index]; }
    int operator[](unsigned index) const { return _array[index]; }

    operator int*() {return _array;}
    operator const int*() const { return _array; }

    myarray(const myarray&) = delete;
    void operator=(const myarray&) = delete;

private:
    unsigned _size;
    int* _array;
};

void mysort(int* arr, unsigned sz) { std::sort(arr, arr + sz); }
void mysort(myarray& arr) { return mysort(arr, arr.size()); }

void display(const myarray& arr)
{
    for (unsigned i = 0; i < arr.size(); ++i)
        std::cout << arr[i] << '\n';
    std::cout << '\n';
}

int main()
{
    myarray arr(10);
    for (unsigned i = 0; i < arr.size(); ++i)
        arr[i] = arr.size() - i;

    display(arr);
    mysort(arr);
    display(arr);
}


http://ideone.com/dS3LX7
Topic archived. No new replies allowed.