Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a get method in C++ for a field that is a fixed size 2D array? [duplicate]

Tags:

c++

c++14

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.

like image 567
David Hadley Avatar asked Jan 10 '19 14:01

David Hadley


1 Answers

The error message tells you the type! The tricky part is the syntax:

double (*getarray())[2] { return dataptr->array; } 

In other words, you almost had it, but you need to bind that * a bit tighter using parentheses.

You can avoid the decay of the outermost dimension:

double (&getarray())[2][2] { return dataptr->array; }

Or you can put this type into an alias to avoid the mess.

Or you can use auto, or switch to std::array to make things easier :P

#include <iostream>
#include <array>

class Data {
public:
    using ArrayType = std::array<std::array<double, 2>, 2>;
    ArrayType array;
};

class Wrapper {
private:
    Data* dataptr;
public:
    Wrapper(Data* data) : dataptr(data) {}

    Data::ArrayType& 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;
}
like image 61
Lightness Races in Orbit Avatar answered Oct 21 '22 06:10

Lightness Races in Orbit