1

I've just tried:

class Test
{

public:
        int iArray[][];

}

...is this not possible? Do I have to set a constant value?

like:

class Test
{
public:
       const int iArray[5][4];
}

I want to define [x][y] later, just have the placements there. Else it wouldn't be "dynamic" and I don't want to use a vector because I want to be able to access the values by "X" and "Y".

6
  • Did you try it? I did, and I got error: declaration of iArray as multidimensional array must have bounds for all dimensions except the first - I think that explains it as well as I can. Commented Aug 17, 2012 at 11:45
  • 1
    std::vector has a [] operator. Commented Aug 17, 2012 at 11:48
  • Please show us a scenario of how you want to use this class. What is X and Y? Commented Aug 17, 2012 at 11:50
  • Test.iArray[x][y]. If I before runtime want to define a grid of somekind I don't want it to be set by a #define value or a CONST value, it should be define at runtime according to user preference. Commented Aug 17, 2012 at 12:02
  • 1
    I cannot think of a reason you shouldn't be using a vector here. For novice C++ programmers, except for instructional purposes, or when you know the exact container size, there's absolutely no reason to be using 'naked' arrays over vectors (which are essentially array structures where all the memory management is handled for you). Commented Aug 17, 2012 at 12:52

6 Answers 6

4

I think better way to achieve this is to use pointers. You can do like this.

#include <cstdlib>
#include <iostream>

using namespace std;

class PointerTest {

    private:
        int** array;
        int x, y;
    public :
        void setValue(int row, int col,int value);
        int getValue(int row, int col);
        PointerTest(int row, int col);
       ~PointerTest() {
    for(int i=0;i<x;i++) {
        delete array[y];
    }
        }


};
PointerTest::PointerTest(int row, int col) {
    x=row, y=col;
    for(int i=0;i<row;i++) {
        *array=new int[col];
    }
}


void PointerTest::setValue(int row, int col, int value) {
    *(array[row])=value;
}

int PointerTest::getValue(int row, int col) {
    return *(array[row]);
}

int main(int argc, char *argv[])
{
    PointerTest* t=new PointerTest(4,5);
    t->setValue(0,0,464);
    cout<<"The value in array: "<<t->getValue(0,0)<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
Sign up to request clarification or add additional context in comments.

Comments

3

What about

tempalte <int N1, int N2> class Test
{
public:
  int iArray[N1][N2];
};

?

4 Comments

Yes, this would work. I really don't want to use a vector for it. Thanks.
But please note the difference, many classes (instances of this template) will be created at compile time.
Whats the difference for performance? I'm creating a game and want it to be customizable, therefor nothing "static" and definitive.
I would suggest that you first check that this allows the required level of flexibility. Then you should see if there is a performance loss. But I don't think it will run slower than the solution with vectors. It could increase the amount of stack space used if you have many local variables of type Test.
3

What about putting a std::vector in a vector?

std::vector< std::vector< const int > > iArray;

There aren't many reason to use "plain" arrays in C++.

1 Comment

That's not quite the same thing: it allows raggedness (which int[][] doesn't), and it has a separate allocation per line (which can also affect locality). Writing a simple matrix class based on std::vector<int> is not rocket science, however.
2

If you want to decide int iArray[][]; size later then you can use vector< vector<int> > iArray;.

The other way is to use nested new[], which would be little complex.

Comments

2

No this is not possible. But you can have a pointer in your class like

int **ptr;

and then in the constructor or where ever allocate the memory for your array with

ptr = (int **)malloc( the size you want );

or with the "new[]"-operator in C++.

but if you are using C++ .. the best way is to use:

std::vector< std::vector< int >> array;

Comments

1
class Test
{
public:
  Test()
  {
    iArray = new int*[5];
    for(int i = 0; i < 5; i++)
      iArray[i] = new int[4];
  }
  ~Test()
  {
    for(int i = 0; i < 5; i++)
      delete[] iArray[i];
    delete[] iArray;
  }
  int** iArray;
};

Will allow you to allocate a 2d int array at runtime (in this example it is a 5x4), but in all honestly I would use vectors as pointed out by some other posters, you don't need to worry about freeing the memory afterwards like you do with the use of new.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.