|
-
March 2nd, 2004, 12:57 PM
#1
CArray - is it a bit limited??
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 2nd, 2004, 01:00 PM
#2
Each member of the CArray could be another array.
-
March 2nd, 2004, 01:03 PM
#3
Oooh - not a bad idea. I'll have a think about that!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 2nd, 2004, 01:55 PM
#4
Originally posted by John E
Oooh - not a bad idea. I'll have a think about that!
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 =).
Code:
#include <afxtempl.h>
int main()
{
CArray<int, int> A1;
CArray<int, int> A2 = A1; // error
CArray<int, int> A3;
A3 = A1; // another error
}
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:
typedef CArray<int, int> C1D;
typedef CArray<C1D, C1D> C2D;
will generate an error since C1D is not copyable.
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
-
March 2nd, 2004, 02:29 PM
#5
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 ???
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 2nd, 2004, 02:35 PM
#6
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
Last edited by souldog; March 2nd, 2004 at 02:39 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
March 2nd, 2004, 02:46 PM
#7
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 2nd, 2004, 02:56 PM
#8
Code:
myArray[2][5] = 67;
See folowing for making a 3 dimensional array using CArray
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.
-
March 2nd, 2004, 03:00 PM
#9
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...!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 2nd, 2004, 03:12 PM
#10
Originally posted by John E
Another convert to STL...!
DEFENDER OF ALL THINGS STL!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|