CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    Each member of the CArray could be another array.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    Oooh - not a bad idea. I'll have a think about that!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    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

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    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

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    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

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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.

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    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

  10. #10
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    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
  •  





Click Here to Expand Forum to Full Width

Featured