-
October 10th, 2003, 10:08 AM
#1
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
-
October 10th, 2003, 10:11 AM
#2
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
-
October 10th, 2003, 10:13 AM
#3
I believe kuphryn's answer will work equally well for CLists...
bytz
--This signature left intentionally blank--
-
October 10th, 2003, 10:14 AM
#4
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
-
October 10th, 2003, 10:18 AM
#5
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
-
October 10th, 2003, 10:23 AM
#6
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
-
October 10th, 2003, 10:24 AM
#7
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--
-
October 10th, 2003, 10:25 AM
#8
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
-
October 10th, 2003, 10:27 AM
#9
Wow, thanks a lot Paul! I now have everything (and more!) I need to know to do what I want.
Thanks again,
Maxime
-
October 10th, 2003, 10:29 AM
#10
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
-
October 10th, 2003, 10:45 AM
#11
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--
-
October 10th, 2003, 10:57 AM
#12
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
-
October 13th, 2003, 04:56 AM
#13
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
-
October 13th, 2003, 07:26 AM
#14
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;
-
October 13th, 2003, 07:46 AM
#15
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
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
|