Re: problems with std::list
How much code relies on CPtrList? I only ask because, unless the amount is huge, you'd be better off biting the bullet and doing it "properly" - i.e. the STL way and not with Micro$soft's barmy, half-arsed containers.
Point 1 - std::list<>::iterator is not necessarily a pointer (in fact, it probably isn't), so assigning NULL (or, properly, 0) to it is not guaranteed to work. You might be able to get away with using list::end() instead of NULL, but that's going to need an object to get the end() from.
Point 2 - lists of void* are decidedly iffy. The point of std::list<> is to have a list of things that you want, not some ridiculous toy container like CPtrList, so storing void* in it is a bit like using an antique enamelled chinese box to store your old socks in.
No, you're embarking on a path of pain, frustration and premature baldness. My advice is to forget trying to emulate a bad container by using a good one, and to use the good container properly.
Sorry, that's probably not what you wanted to hear, but it's my honest advice.
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
Re: problems with std::list
As Graham pointed out, is it worth all of this effort? Just use std::list<> and be done with it.
IMO, the CPtrList class was created so that MFC could coverup for its mistakes with CObList class. As a matter of fact, if you insisted on staying with MFC, you should have chosen the newer CList template class, and just make the template type a T*. If you're going to use MFC, CPtrList is the worst class you could have chosen. It is not typesafe, you need to cast all over the place to get the right value, etc. etc.
Having said this, reconsider what you are doing here. You used CPtrList to store pointers -- do you really need to store pointers? A major idea of C++ is typesafety. There is absolutely *no* need for you to be storing void pointers. From the looks of your code, it sounds like you are not familiar with writing template functions. If you were, you wouldn't have even wrote std::list<void*>, instead you would have written std::list<T*>, where T is a template argument.
If I were to write a replacement for CPtrList, it would look something like the following:
#include <list>
template <class T>
class CStdPtrList
{
public:
typedef std::list<T*>::iterator POSITION;
//...
private:
std::list<T*> m_List;
public:
POSITION GetHeadPosition() { return m_List.begin(); }
T* GetNext(POSITION& p)
{
if ( p == m_List.end() )
return NULL;
return *(++p);
}
}
I didn't compile the code above, but this is what I would have expected. Note how POSITION is an iterator type. As far as your NULL code -- get rid of it. There is no such thing as a NULL iterator.
Regards,
Paul McKenzie
Re: problems with std::list
Yeah your both right, it is a pain in the bum. A Bit of Background, I've in inherited code which is over 260,000 lines, and there are around 2000 CptrLists, nice isn’t it. I need to port this code to unix so in my opinion writing a wrapper class is going to be the best idea.
I’ve sorted it now, basically you need to position to be a separate class, because you need to rewrite a load of the operators, so just use a typedef won’t work. Trust me this has taken 2 weeks so far and I’ve tried everything.
To fix it I’ve created a bool value as a member variable of my POSITION class, so where as before I was setting the iterator to NULL, now I’m setting the bool to false, it works, it’s not nice but it works….
Cheers for the advice anyway…