Solved

Multidimensional CArrays?

Posted on 2004-04-20
7
856 Views
Last Modified: 2013-11-20
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
Comment
Question by:gamesdev
7 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10867840
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
 
LVL 3

Expert Comment

by:akalmani
ID: 10867851
Try this...

typedef CArray<int, int> rgInt;
CArray<rgInt, rgInt> rgTest;
0
 
LVL 3

Accepted Solution

by:
akalmani earned 100 total points
ID: 10867891
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:leflon
ID: 10867898
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
 
LVL 7

Assisted Solution

by:leflon
leflon earned 50 total points
ID: 10867934
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
 
LVL 1

Author Comment

by:gamesdev
ID: 10867988
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
 
LVL 30

Expert Comment

by:Axter
ID: 10883220
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 76
notReplace  challenge 53 116
ffmpeg - "rtsp://...... Operation not permitted" 4 74
zeroFront challenge 7 80
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now