[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 881
  • Last Modified:

Multidimensional CArrays?

Hi,
How can one represent multidimensional arrays using CArray objects?
I tried this with both CList and CArray:

typedef CList<int> CIntList; //Okay!
typedef CArray<CIntList> CArrayOfIntList; //Okay!

CArrayOfIntList x;

x.Add(CList()); //Error!

CIntList y;
x.Add(y); //Error!

What should the syntax be? is it even possible??

Thanks
0
gamesdev
Asked:
gamesdev
2 Solutions
 
leflonCommented:
Hi gamesdev,
i may be wrong but the definition of CArrayOfIntList should be

  typedef CArray<CIntList,CIntList> CArrayOfIntList;
or
  typedef CArray<CIntList, CIntList&> CArrayOfIntList;


leflon
0
 
akalmaniCommented:
Try this...

typedef CArray<int, int> rgInt;
CArray<rgInt, rgInt> rgTest;
0
 
akalmaniCommented:
I missed the usage of it....

typedef CArray<int, int> rgInt;
CArray<rgInt*, rgInt*> rgTest;

rgInt *pInt = new rgInt;
pInt->Add(1);
rgTest.Add(pInt);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
leflonCommented:
gamesdev,

both CList and CArray need two arguments in the definition.
CList<int,int>
CArray<CIntList,CIntList&>

> x.Add(CList()); //Error!
will produce an error as only CIntList objects can be added to x

leflon
0
 
leflonCommented:
gamesdev,

and you will (at least i got one) get an syntax error saying that list  =operator is unavailable.
so you may want to use akalmani's way or derive your own List class from CList, providing the needed =operator.

leflon
0
 
gamesdevAuthor Commented:
Hi leflon,
They don't, the second parameter has a default value of a reference to the first parameter. (as far as I know, I am using VS.net 2003)

And the problem is with the =operator like you mentioned, so I guess I'll use akalmani's method.

Thanks guys.
0
 
AxterCommented:
I recommend you use something like the following class:
template < class T>
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(){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;
};

The above class is easier to use, and more efficient.
Example:
int col = 3, row = 4;
dynamic_2d_array < int > My2dArray(col, row);

My2dArray[1][2] = 99;
cout << My2dArray[1][2] << endl;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now