Am I right in thinking that the MFC class CArray can only support a one-dimensional array? If so, is there any way to construct a multi-dimensional array using MFC?
Printable View
Am I right in thinking that the MFC class CArray can only support a one-dimensional array? If so, is there any way to construct a multi-dimensional array using MFC?
Each member of the CArray could be another array.
Oooh - not a bad idea. I'll have a think about that! :thumb:
The problem with that is CArray requires that its elements are copyable. Since the element is another CArray, a CArray is not copyable without having to write a copy constructor (and operator =).Quote:
Originally posted by John E
Oooh - not a bad idea. I'll have a think about that! :thumb:
The copy and assignment are what is done internally inside of the CArray class, and the code above fails to compile when you are trying to copy and assign CArrays. Therefore doing this:Code:#include <afxtempl.h>
int main()
{
CArray<int, int> A1;
CArray<int, int> A2 = A1; // error
CArray<int, int> A3;
A3 = A1; // another error
}
will generate an error since C1D is not copyable.Code:typedef CArray<int, int> C1D;
typedef CArray<C1D, C1D> C2D;
As to your general question as to "CArray - is it a bit limited?", you will get a "Yes" answer to those who have used std::vector instead of CArray. A std::vector of vectors needs no copy constructor, since vector is copyable "out of the box".
Regards,
Paul McKenzie
Thanks, Paul. In fact I just took a look at Gabriel's tutorial which was quite helpful. This might seem like a dumb question but what would be the syntax for declaring a 2 dimensional array of ints, say, using std::vector? Would it have to be done like this:-
std::vector<int> oneDimensionalArray;
std::vector<oneDimensionalArray> twoDimensionalArray;
or is that being silly ???
you would need to use a typedef on the first one
typedef std::vector<int> oneDimensionalArray;
std::vector<oneDimensionalArray> twoDimensionalArray;
or
std::vector<std::vector<int> > twoDimensionalArray;
both of these declare an instance of std::vector<std::vector<int> >
called twoDimensionalArray.
or you could use typedef on both
typedef std::vector<int> oneDimensionalArray;
typedef std::vector<oneDimensionalArray> twoDimensionalArray;
twoDimensionalArray YourArray_;
then twoDimensionalArray is a type and YourArray_ is an instance
Thanks for that. Now for the 64,000 dollar question....
With a one dimensional array, I just say:-
myArray[5] = 67;
but if 'myArray' is two dimensional, how do I set the value at element 2,5 to be 67?
See folowing for making a 3 dimensional array using CArrayCode:myArray[2][5] = 67;
and vector :
http://www.codeguru.com/forum/showth...ghlight=CArray
Note: using "array of arrays" as above will result in memory
that is not contiguous. Some API's (such as OpenGL) expect
the multi-dimensional arrays to be contigous. In that case
I have a simple class that internally uses a one dimensional
array, when the user asks for (i,j), it calculates the index
into the 1D array and uses that element. I don't have that
on this computer, but if interested, I can post it tomorrow.
Wow - so it's pretty much as simple as using a fixed size array, except that it's growable. That's impressive!
Another convert to STL...! :cool:
:thumb:Quote:
Originally posted by John E
Another convert to STL...! :cool: