CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Aug 2003
    Posts
    30

    How to create a list of lists using the CList template class

    Hi all,

    I think the subject mostly says it all :-). I need to create a list of lists, and since the rest of the code is using the CList template class, I'd like to use it for that too if at all possible. However, I don't get how I could do this. Could anyone with more clue than me on this topic help me out?

    Thanks in advance,
    Maxime

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    I am not familiar with MFC's CList. I recommend C++ STL. Here is one example of the list container.

    typedef std:list<int> listInt;
    typedef std::list<listInt> llInt;

    Kuphryn

  3. #3
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    I believe kuphryn's answer will work equally well for CLists...
    bytz
    --This signature left intentionally blank--

  4. #4
    Join Date
    Aug 2003
    Posts
    30
    Originally posted by kuphryn
    I am not familiar with MFC's CList. I recommend C++ STL. Here is one example of the list container.

    typedef std:list<int> listInt;
    typedef std::list<listInt> llInt;

    Kuphryn
    Thanks for the pointer, it'll probably be useful to me. However, for this particular project I'm working on, C++ STL is not an option.

    Cheers,
    Maxime

  5. #5
    Join Date
    Aug 2003
    Posts
    30
    Originally posted by bytz
    I believe kuphryn's answer will work equally well for CLists...
    I tried doing something similar, but I then had compilation errors inside the CList implementation. I tried to do it this way:

    typedef CList<mytype_t, mytype_t> CMyTypeList;

    and then:

    CList<CMyTypeList, CMyTypeList> foo;


    Maybe I'm doing something stupid here?

    Cheers,
    Maxime

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to create a list of lists using the CList template class

    Originally posted by mux
    Hi all,

    I think the subject mostly says it all :-). I need to create a list of lists, and since the rest of the code is using the CList template class,
    Too bad. If you used std::list<>, it would have been much easier to create a list of lists.
    I'd like to use it for that too if at all possible. However, I don't get how I could do this.
    To create a CList of a CList you must inherit your own class from CList and provide a user-defined assignment operator. The reason for this is that the types that you place in CList must be assignable. In other words, this code isn't going to work:
    Code:
    #include <afxtempl.h>
    
    int main()
    {
       CList<int, int> a;
       CList<int, int> b;
       //
       a = b;  //  will not compile
    }
    Since you cannot assign CLists, you can't make CList's as types for a CList without providing an assignment operator. In the assignment operator, you have to write code that copies from one CList to another.

    The other alternative, if you still want to use CList, is to create a CList of pointers to a CList. This will work, but then there is the extra overhead and maintenance of your code having to make sure that the pointers are valid, and allocated / destroyed correctly.
    Code:
    typedef CList<int, int> IntList;
    typedef CList<IntList*, IntList*> CListOfIntList;
    //...
    IntList IL = new IntList;
    //...
    CListOfIntList CL;
    CL.Add( IL );
    The last alternative is to use the standard container classes:
    Code:
    #include <list>
    std::list< std::list<int> > ListOfLists;
    That's all you need to do to create a list of a list of ints. You don't need to provide an assignment operator, and you don't need to resort to pointers. That's why I mentioned that it is too bad you used CList.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    I only "foggily" remember making CLists of CLists; the two thoughs that come to mind are to create another class that contians the list and make a CList of that or use a list of pointers i.e:
    CList<CMyTypeList *, CMyTypeList*> foo;
    bytz
    --This signature left intentionally blank--

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by mux
    However, for this particular project I'm working on, C++ STL is not an option.
    Just curious. Why isn't STL an option?

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2003
    Posts
    30
    Wow, thanks a lot Paul! I now have everything (and more!) I need to know to do what I want.

    Thanks again,
    Maxime

  10. #10
    Join Date
    Aug 2003
    Posts
    30
    Originally posted by Paul McKenzie
    Just curious. Why isn't STL an option?

    Regards,

    Paul McKenzie
    Well, since the rest of the code isn't using STL, I wouldn't like to start using it just for this little piece of code. I believe that it would cause my executable to get bigger, wouldn't it?

    Cheers,
    Maxime

  11. #11
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    Originally posted by mux
    Well, since the rest of the code isn't using STL, I wouldn't like to start using it just for this little piece of code. I believe that it would cause my executable to get bigger, wouldn't it?
    It will add some size but not significantly.
    bytz
    --This signature left intentionally blank--

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by mux
    Well, since the rest of the code isn't using STL, I wouldn't like to start using it just for this little piece of code. I believe that it would cause my executable to get bigger, wouldn't it?

    Cheers,
    Maxime
    If you eliminated the usage of the MFC containers, the code may be smaller, not larger.

    If all you use MFC for are container and string classes, I know for sure that your code will be significantly smaller if you replaced MFC containers and strings with STL containers.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Aug 2003
    Posts
    30
    Hi again,

    I've finally decided to use the STL standard list template because it's much simpler. But now I'm _really_ confused. I properly #include <list> at the beginning of my source file, and then when I try to declare a list by using std::list<type>, the compiler tells me that list is not part of the std namespace. I also tried to use the using namespace std; directive and just using list afterwards, but the compiler still can't find the list identifier.

    I am using Visual C++ 7.0. Is this a known bug or am I doing something stupid?

    Cheers,
    Maxime

  14. #14
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    The order is :

    Code:
    #include <list>
    
    using std::list; // or using namespace std;
    
    
    // or do not use any "using" and qualify at declaration time:
    
    std::list<int> my_list;

  15. #15
    Join Date
    Aug 2003
    Posts
    30
    Originally posted by Philip Nicoletti
    The order is :

    Code:
    #include <list>
    
    using std::list; // or using namespace std;
    
    
    // or do not use any "using" and qualify at declaration time:
    
    std::list<int> my_list;
    I was doing :

    #include <list>

    and then declaring as "std::list<mytype> mylist;" directly.

    I tried with using namespace std; and with using std::list; and then declaring as "list<mytype> mylist;" but it doesn't change anything, it's still saying that list is not a member of std.

    Thanks,
    Maxime

Page 1 of 2 12 LastLast

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