4

I have a C++ class that has a function that takes in a 2D array from another variable of the same class so that this particular instance of the array can access the other variable. The variable in question is a 2D array. Given the code below how would you implement the accessor function to get the private 2D array variable. My intention is for the accessor to return the 2d array but I cannot figure out which return type to use. int[][], int**, int* and int doesn't work.

class MyClass{
  public:
    // gets private x variable from another MyClass variable
    void proccessVar(const MyClass& aVar){
      // get and process the 2D array by calling getX()
      // int temp[10][10];
      // temp = aVar.getX();
      // proccess temp 2D array
    }

    // Return 2D array x
    int** getX(){
      return x;
    }

  private:
    int x[10][10] = {{0},{0}}; // initialized value
}
TheWinterSnow
  • 175
  • 11
  • 1
    Any reason not to use std::array? – spectras Nov 21 '18 at 10:19
  • 1
    Why doesn't `int**` work? – Korni Nov 21 '18 at 10:21
  • 1
    Possible duplicate of [Return a 2d array from a function](https://stackoverflow.com/questions/8617683/return-a-2d-array-from-a-function) – Korni Nov 21 '18 at 10:21
  • 1
    @Korni on the return x I get the error: cannot convert ‘int (*)[10]’ to ‘int**’ in return. – TheWinterSnow Nov 21 '18 at 10:23
  • 3
    @Korni because `x` when decayed to a pointer is not "a pointer to a pointer to int" type but it's "a pointer to array of 10 ints". `int**` is different from `int (*)[10]`. In case `int **a; a[1][2]` the compiler does `*( *(a + 1) + 2)` In case `int (*a)[10]; a[1][2]` then compiler does `*( a + 1 * 10 + 2 )`. – KamilCuk Nov 21 '18 at 10:23
  • @spectras I forgot about std::array but the code I am writing is a prototype/mock-up of a class assignment that requires that the code is in a mix of Bash, Perl and Python scripting so I am trying to not use C++ specific code. I am mocking up the code in C++ because I am most comfortable getting around the logic and getting a basic proof of concept. – TheWinterSnow Nov 21 '18 at 10:26
  • @TheWinterSnow Trying to juggle with pointers in this way is much more C++ specific than using std::array. Converting the mockup to python,etc. would be much easier if you use std::array in the first place, imo. – eozd Nov 21 '18 at 10:38
  • @eozd I was initially trying to mock-up the project in java but was having problems with 2d arrays in Java because it has been a long time since I really coded in Java. I know nothing of Python and have a massive project due that has to be in Python but my class has been delayed because of the massive forest fires in California so I will have about 1 week to learn Python and rewrite the code. Yeah I am definitely in the dark at the moment. – TheWinterSnow Nov 21 '18 at 10:51
  • If you want to work parallel in python, I'd even use `std::vector`, as that one behaves the closest to arrays in python. Python arrays are dynamic by default and have iterators, and `for(auto& row : table)` is as close as it gets to `for row in table :`. – Aziuth Nov 21 '18 at 11:12

2 Answers2

3
int x[10][10]

x is an two-dimensional array of 10 for 10 ints.

Let's decay to a pointer:

int (*y)[10] = x;

y is a pointer to an array of 10 ints.

You should return a pointer to an array of 10 ints:

int (*getX())[10] {
  return x;
}

Or rethink you approach and use std::array.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    OP could also return `int(&)[10][10]` (or equivalently `decltype(auto)`) and keep the size information. – Quentin Nov 21 '18 at 10:38
1

The existing answer (return a pointer to 1-D array) works, but the following may be less confusing.

The idea is to return a reference to the 2-D array. The syntax is:

class MyClass
{
    ...
    int (&getX())[10][10]
    {
        return x;
    }
};

Or, using a typedef:

using myarray = int[10][10];
class MyClass
{
    ...
    myarray& getX()
    {
        return x;
    }
};

All these workarounds are because C++ cannot return an array from a function/method.

anatolyg
  • 26,506
  • 9
  • 60
  • 134