Collection Class Question
-
Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)
Why do you need to write your own collection ? What are you making a container of ? I guess pointers to some MFC type ? There seem to be some examples in MSDN for this, go to msdn.microsoft.com if you don't have MSDN on your PC. Personally, I'd use std::vector, but anyone know knows me remotely knew I'd say that. I'm not familiar with any benefits of CObList, but if it's a container specialised for MFC types, there may well be some, in which case I can't help too much except recommending you check the MSDN. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002
-
Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)
If what you mean is how to have a
CObList
of 100 elements, you would have to insert them one by one usingCObList::AddTail
. The following restrictrions apply:- What you store are not
CObject
s, but pointers toCObject
created withnew
(CObList
assumes responsibility fordelete
ing them later). In particular, you cannot store simple types likeint
s orstruct
s not deriving fromCObject
. - When you retrieve an object stored in a
CObList
(well, a pointer to it), you have to figure out its actual type and downcast (most usually what you store are notCObject
s but some other class derive from this type.)
If you don't mind me evangelizing just a little about STL, why don't you give
std::vector
orstd::list
a try? They are way more robust than MFC containers, platform-independent and a lot of fun :) Plus, Christian Grauss is writing a series of tutorials on STL to help newbies make a warm start. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo - What you store are not
-
If what you mean is how to have a
CObList
of 100 elements, you would have to insert them one by one usingCObList::AddTail
. The following restrictrions apply:- What you store are not
CObject
s, but pointers toCObject
created withnew
(CObList
assumes responsibility fordelete
ing them later). In particular, you cannot store simple types likeint
s orstruct
s not deriving fromCObject
. - When you retrieve an object stored in a
CObList
(well, a pointer to it), you have to figure out its actual type and downcast (most usually what you store are notCObject
s but some other class derive from this type.)
If you don't mind me evangelizing just a little about STL, why don't you give
std::vector
orstd::list
a try? They are way more robust than MFC containers, platform-independent and a lot of fun :) Plus, Christian Grauss is writing a series of tutorials on STL to help newbies make a warm start. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo - What you store are not
-
Say I have an array Stuff[100] how would I make that into a collection class I'm having trouble understanding this. I need to derive it from the CObList in MFC. Thanks for your time guys. -CDudd:)
Ditto on the STL stuff. I might have some strong reservations about many of the STL features, but the containers are GOLDEN... droooool... Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
-
Well for a class to use the CObList it has to be derived from CObList. So, if I made a CStuff class how would I derive and, use it to make it like an array? -CDudd I don't know how to use STL so that is why I don't use it. :)
Maybe I'm misunderstanding you here. What are you trying to do?
- Create a list of 100
CStuff
objects. - Define
CStuff
to behave as aCObist
of fixed size (100). - Other.
I don't know how to use STL so that is why I don't use it. Don't mean to be offensive, but seemingly you don't know how to use
CobList
either and yet you're trying to. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo - Create a list of 100
-
Maybe I'm misunderstanding you here. What are you trying to do?
- Create a list of 100
CStuff
objects. - Define
CStuff
to behave as aCObist
of fixed size (100). - Other.
I don't know how to use STL so that is why I don't use it. Don't mean to be offensive, but seemingly you don't know how to use
CobList
either and yet you're trying to. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloI need a CStuff class to use all the functions of CObList so I can create a list of 100 CStuff objects. -CDudd No offense taken, your right I don't quite understand CObList...yet, but it's just one class others I know how to use. Since I don't know anything at all about STL I don't use it. ;)
- Create a list of 100
-
I need a CStuff class to use all the functions of CObList so I can create a list of 100 CStuff objects. -CDudd No offense taken, your right I don't quite understand CObList...yet, but it's just one class others I know how to use. Since I don't know anything at all about STL I don't use it. ;)
If I'm getting your point now, what you need to do is having
CStuff
derive fromCObject
:class CStuff: public CObject
{
... // as before
};Now say your list of
CStuff
s is called (in a fit of originality)stuffList
. So, to enter a new object (up to 100, if that's your wish) you just write:stuffList.AddTail(new CStuff(/*args here if ctor needs them */));
Hope this is what you were after. Please tell us if it is not. As for the STL stuff, do not be frightened by its rather awesome aspect. In fact, having a
std::list
ofCStuff
s is no harder than withCObList
(and you won't have to derive fromCObject
):std::list<CStuff> stuffList;
...
stuffList.push_back(CStuff()); // no new here, we store objects rather than pointers to themJoaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
If I'm getting your point now, what you need to do is having
CStuff
derive fromCObject
:class CStuff: public CObject
{
... // as before
};Now say your list of
CStuff
s is called (in a fit of originality)stuffList
. So, to enter a new object (up to 100, if that's your wish) you just write:stuffList.AddTail(new CStuff(/*args here if ctor needs them */));
Hope this is what you were after. Please tell us if it is not. As for the STL stuff, do not be frightened by its rather awesome aspect. In fact, having a
std::list
ofCStuff
s is no harder than withCObList
(and you won't have to derive fromCObject
):std::list<CStuff> stuffList;
...
stuffList.push_back(CStuff()); // no new here, we store objects rather than pointers to themJoaquín M López Muñoz Telefónica, Investigación y Desarrollo
I've been trying for about an hour or so and I'm royally stuck. Basically I've got a class CShapePt. The program is like a mini paint utility. Whenever the user clicks on the client area it takes the point clicked. Then the next time he/she clicks it draws a line between the two places. The Array it was using was in the MainFrame Header file. I'm not exactally sure how to put the CShapePt object into the MainFrame header file so that the device context can access it. How do you do those code highlighter things? If you need some code to help you see what I'm having trouble explaining I'll be glad to put some snippets on here. Thanks. -CDudd