I need to write a wrapper class that provides "get" methods for the fields of a data class. However, I don't know the syntax to write a "getter" to return a 2D array. For example:
#include <iostream>
class Data {
public:
double array[2][2];
};
class Wrapper {
private:
Data* dataptr;
public:
Wrapper(Data* data) : dataptr(data) {}
// compile error: "cannot convert ‘double (*)[2]’ to ‘double**’ in return"
double** getarray() { return dataptr->array; }
// compile error: "‘getarray’ declared as function returning an array"
//double* getarray()[2] { return dataptr->array; }
// this works, but what is auto resolved to?
//auto getarray() { return dataptr->array; }
};
int main() {
Data d;
d.array[0][0] = 1;
d.array[0][1] = 2;
d.array[1][0] = 3;
d.array[1][1] = 4;
Wrapper w(&d);
auto arr = w.getarray();
return 0;
}
I know that it is possible as I set the method return type to auto and it compiles and runs as expected. But I don't know how to write the method with an explicit return type.
In my real world example, I cannot modify the Data class so switching to using std::vector is not an option.