Making a 2D array dynamically inside a class

I want to make a 2D array with values I set inside a class (using the constructor). How would I go about doing something like that?

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class planet {
public:
     planet(int, int);
     map[][]; //<-- not valid, but...
     string type, name;
};

planet::planet(int x_size, int y_size) {
     this->map[x_length][y_length] //<-- also not valid
     this->type = "null";
     this->name = "Default";
}

int main() {
     planet Earth(100,100); //I want this to make a planet Earth with Earth.map be 100*100 array
}
Last edited on
You haven't given much away on what you want to do with this class so.....
The way I see it you have three options:
You could use an STL container. With an STL container e.g. std::vector you wouldn't necessarily worry about how many entries it can hold because it sorts that out itself, within (very big) limits. If you needed to limit the size of data then you could use two member variables to hold x_size and y_size and test against them each time you accessed the container. The containers gives you various search, insert and delete capabilities depending on which one you use.

You could use a pointer and malloc/new. It would allow you to control exactly how much memory you use to store your data and is quite simple programmatically. You can't really use the [] array index access technique directly to access your data although you could start being clever and create you own container type class with a [] operator. You would move around your data using pointers.

You could create a linked list. This is probably the most complicated programmatically. Again you can control the size like malloc/new but with a linked list you can search, insert and delete more easily than the malloc/new method.
closed account (z05DSL3A)
Could you do somthing along the line of this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <iostream>
#include <string>

/*****************************************************************************/
class planet 
{
public:
     planet(int, int);
	~planet();
     
	 int **map;

private:
	int **Allocate2DArray( int nRows, int nCols);
	void Free2DArray(int** Array);
};

/*****************************************************************************/
planet::planet(int x_size, int y_size) {
     this->map = Allocate2DArray( x_size,y_size);
}

/*****************************************************************************/
planet::~planet()
{
	Free2DArray(map);
}

/*****************************************************************************/
int **planet::Allocate2DArray( int nRows, int nCols)
{
    //(step 1) allocate memory for array of elements of column
    int **ppi = new int*[nRows];

    //(step 2) allocate memory for array of elements of each row
    int *curPtr = new int [nRows * nCols];

    // Now point the pointers in the right place
    for( int i = 0; i < nRows; ++i)
    {
        *(ppi + i) = curPtr;
         curPtr += nCols;
    }
    return ppi;
}

/*****************************************************************************/
void planet::Free2DArray(int** Array)
{
    delete [] *Array;
    delete [] Array;
}

/*****************************************************************************/
int main() {
     planet Earth(100,100); //I want this to make a planet Earth with Earth.map be 100*100 array

     Earth.map[0][0] = 10;
}
Last edited on
Dang, I knew I was going to have to get into allocating/deallocating memory and pointers...XD Thanks for your help bnbertha/Grey Wolf, I was thinking about it last night and thought about something like that, but your code made it very clear. Thanks again.
Topic archived. No new replies allowed.