pointer & array question
see line above and below error C2440. how to get this compile and run?.
template < class T, int ROW_T = 0, int COL_T = 0 >
class dynamic_2d_array
{
public:
dynamic_2d_array(int row, int col):m_row(row),m_col(col), m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
dynamic_2d_array():m_row(ROW_T),m_col(COL_T), m_data(new T[ROW_T*COL_T])
{//The following code prevents the default constructor from being used without defineing ROW_T and COL_T
if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] = {{x[0]}};}
}
~dynamic_2d_array(){if(m_data) delete []m_data;}
inline T* operator[](int i) {return (m_data + (m_col*i));}
inline T const*const operator[](int i) const {return (m_data + (m_col*i));}
private:
const int m_row;
const int m_col;
T* m_data;
};
int main()
{
m_size = m_width * m_Height;
dynamic_2d_array < int > My_dynamic_2d_array(1024, 1024);
int *pSrc = My_dynamic_2d_array;
error C2440: 'initializing' : cannot convert from 'class dynamic_2d_array<int,0,0>' to 'int *'
No user-defined-conversion operator available that can perform this conversion, or
the operator cannot be called
int *pSrcEnd = My_dynamic_2d_array + m_size;
while(pSrc < pSrcend)
{
//Something
pSrc++;
}
return 0;
}
[1494 byte] By [
nanos] at [2007-11-18 19:27:59]

# 1 Re: pointer & array question
My first impression of your code was that you wanted to iterate over an array of type My_dynamic_2d_array. After a closer look, I see you want to iterate over the array within the object. This means you need to provide a getData() template function to get the array pointer within the object, then use that to iterate over.
Ignoring the fact that you got your pointers mixed up (the pointer to object My_dynamic_2d_array, is not the same as the pointer to My_dynamic_2d_array->m_data), the reason your compiler doesn't like the line of code, is that it tells the compiler that you want to assign the address My_dynamic_2d_array to an int pointer. While the fact that you're assigning a pointer to a pointer seems fine on the surface, the compiler doesn't like this for cases just like the sort of thing you do in your code:
pSrc++;
That line tells the compiler to increment the pointer the same number of bytes as is allocated for the pointer's data type, in this case "int", which is typically 4-bytes on most systems. Your class is 2 int's and a pointer...at least 12-bytes on a typical system (maybe with some added overhead as well). See the problem?
You [I]can[/] try overriding the pointer operator, but that'll probably get you into trouble if you want to work with the actual pointers to the objects, rather than the pointers to the enclosed data array. So, unless you can think of a really clever way to pull it off, stick with the approach below:
*T getData() const
{
return m_data;
}
# 2 Re: pointer & array question
i could not implement this approach:
*T getData() const
{
return m_data;
}
could you elaborate?.
like how to retrieve m_data from a main()?.
nanos at 2007-11-9 0:33:39 >

# 3 Re: pointer & array question
OK, maybe I was too inspecific...
getData() should be a public function in your template class, which means you should be able to do something like
My_dynamic_2d_array.getData()
to get the underlying array.
Oh, and that should be T* not *T... my bad. It's probably OK to make it inline, too.
Last point, you're iterating through the array in main(), but you're not using your index operator you defined...any reason?
# 4 Re: pointer & array question
Originally posted by jefranki
OK, maybe I was too inspecific...
getData() should be a public function in your template class, which means you should be able to do something like
My_dynamic_2d_array.getData()
to get the underlying array.
Oh, and that should be T* not *T... my bad. It's probably OK to make it inline, too.
Last point, you're iterating through the array in main(), but you're not using your index operator you defined...any reason?
This class does not need a getData function to access the items in the array.
I'm the original author of this class, and I designed the class to be access via operator[].
Moreover, you can use double index to access items in the array.
Example:
dynamic_2d_array < int > My_dynamic_2d_array(m_width, m_Height);
int *pSrc = &My_dynamic_2d_array[0][0];
This class allows you to simulate an operator[][].
To fetch the data, just use it like you would a C-Style 2D array.
My_dynamic_2d_array[0][0];
Axter at 2007-11-9 0:35:37 >

# 5 Re: pointer & array question
Here's a modified version of the main() function using the correct syntax and access methods for this class.
int m_size, m_width =3, m_Height=4;
int main()
{
m_size = m_width * m_Height;
dynamic_2d_array < int > My_dynamic_2d_array(m_width, m_Height);
int *pSrc = &My_dynamic_2d_array[0][0];
int testvalue = 0;
for (int x = 0;x<m_width;++x)
{
for(int y = 0;y<m_Height;++y)
{
My_dynamic_2d_array[x][y] = testvalue++;
}
}
cout << "Example using index ..." << endl;
int *pSrcEnd = &My_dynamic_2d_array[m_width-1][m_Height];
while(pSrc < pSrcEnd)
{
cout << *pSrc << ", ";
pSrc++;
}
pSrc = &My_dynamic_2d_array[0][0];
cout << endl << "Example using m_size ..." << endl;
pSrcEnd = (&My_dynamic_2d_array[0][0]) + m_size;
while(pSrc < pSrcEnd)
{
cout << *pSrc << ", ";
pSrc++;
}
Axter at 2007-11-9 0:36:39 >
