returning a 2d dynamic array

Hi,

how would i define the following function so it returns the 2d dynamic array? thanks.

/*
void matrix::CreateMatrix (){

int**ptr_matrix;

ptr_matrix = new int *[width]; // initalizes a pointer to pointer

for(int i=0;i<width;i++){

ptr_matrix[i] = new int [height]; // creates an array of pointers

}

}*/
Same way as you do the variable.

int **matrix::CreateMatrix()

The caller also needs to know the array dimensions though.
since ptr_matrix is an int**, your function could return an int**:

1
2
3
4
5
6
7
int** matrix::CreateMatrix()
{
  int** ptr_matrix; 
  //...

  return ptr_matrix;
}


HOWEVER this is a bad idea for 2 big reasons.

1) multidimentional arrays are generally a bad idea. See this thread: http://cplusplus.com/forum/articles/17108/


2) A matrix class should be the matrix. It shouldn't return a matrix.

Think of how you use std::string. You use the string as if it were a string:
 
string mystring = "some string data";


You don't use it to create some other string. That would be awkward, difficult to use, and kind of pointless:

1
2
3
string mystringobj;

char* mystring = mystringobj.CreateString();  // what would be the point of this? 
ok thanks for thr replies,

i understand your point, this my first time working with classes. Would i make the class a matrix by adding this code to the constructor instead?

Also, how could i use the new operator if i want to make it so the user can define as many objects of type matrix at they want? i've tried something like the code below but i think that there must be an easier way?

int number_of_mat;
cout << "How many matricies would you like to define: ";
cin>> number_of_mat;

vector<matrix> MATS(number_of_mat,0); // using a vector to keep track od all of the objects

for(int i=0;i<number_of_mat;i++){

matrix *ptr_mat; // creates a pointer to an object

ptr_mat = new matrix; // creates a new object

MATS[i] = *ptr_mat; // not sure why this like shouldnt be MATS[i] = ptr_mat; without the pointer?
}

thanks again for the help
Last edited on
Would i make the class a matrix by adding this code to the constructor instead?


You would not return the pointer/array from any function. Instead you would have the pointer/array be a member of the class.

1
2
3
4
5
6
7
class matrix
{
private:
  int** data;

//...
};


You could then write member functions which get/set elements in the matrix.

Allocating space for the data would be done in the constructor, yes.

Also, how could i use the new operator if i want to make it so the user can define as many objects of type matrix at they want?


No need to use new if you're using vector. You actually did it in your code:

1
2
3
4
5
6
vector<matrix> MATS(N);  // this does it

// you now have N matrixes:

MATS[0].Set(x,y,3); // set one element of the first matrix
int foo = MATS[1].Get(x,y);  // get one element from the 2nd matrix 



If you want to use new, you'd do it like so:

1
2
3
4
5
matrix* MATS = new matrix[N];

//...

delete[] MATS;  // clean up when done 


But note that it's one or the other. Don't new if you have a vector.
thanks for the advice!

now i just have one question, in your code the .Set(x,y,3), is this a constructor or function or neither (i'm still trying to grasp the difference between the two)?
It would be a method of the class.
method == member function
Topic archived. No new replies allowed.