Solved

Multidimensional CArrays?

Posted on 2004-04-20
7
851 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 49
has22 challenge 11 71
wordmultiple challenge 12 89
Thin secure Windows 10 5 48
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

14 Experts available now in Live!

Get 1:1 Help Now