Solved

Multidimensional CArrays?

Posted on 2004-04-20
7
865 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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