Deukalion
Deukalion

Reputation: 2655

Initialize empty array in class

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

Upvotes: 1

Views: 6010

Answers (6)

Kusum Adhikari
Kusum Adhikari

Reputation: 210

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;
}

Upvotes: 4

Alexander Chertov
Alexander Chertov

Reputation: 2108

What about

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

?

Upvotes: 3

TVOHM
TVOHM

Reputation: 2742

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.

Upvotes: 1

tuxtimo
tuxtimo

Reputation: 2790

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;

Upvotes: 2

iammilind
iammilind

Reputation: 69988

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.

Upvotes: 2

Marcus Riemer
Marcus Riemer

Reputation: 7688

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

Upvotes: 3

Related Questions