But do you realize what was wrong with your original code?
MyPointer *mp = new MyPointerBase(p2[i]);
MyPointer *mp = new MyPointerBase(p2[i]);
To copy to clipboard, switch view to plain text mode
In this line you create a new instance of MyPointerBase using a pointer to another instance of MyPointer. I hope you understand that this is not a copy constructor.
if (p1.isEmpty() || !p1.contains(mp))
if (p1.isEmpty() || !p1.contains(mp))
To copy to clipboard, switch view to plain text mode
In the very next line you ask the QList if it contains the pointer you just created. Of course it doesn't - you just made it, and have not inserted it into the container yet.
I think you have some very basic confusion between object instances and pointers to object instances. Your QList is a container of pointers and therefore any comparison is between the pointers themselves, not the object instances those pointers point to. So your class operator==() will never be called during any QList method that compares members of the list.
Even so, your attempt to overload operator==() is just off track.
// This really isn't a class member at all.
// This operator would be invoked through a call like
// if ( *mp1 == *mp2 )
bool operator== (MyPointerBase &pb1, MyPointerBase &pb2) {
return (pb1.id == pb2.id);
}
// This one is a class member, but it's wrong.
// This one would get called through code like
// if ( *mp1 == mp2 )
// or
// if ( mp1->operator==( mp2 ) )
// which is downright weird. How would your replacement ever understand code like that?
bool MyPointerBase::operator== (const MyPointerBase *other) const {
return (this->id == other->id);
}
// This really isn't a class member at all.
// This operator would be invoked through a call like
// if ( *mp1 == *mp2 )
bool operator== (MyPointerBase &pb1, MyPointerBase &pb2) {
return (pb1.id == pb2.id);
}
// This one is a class member, but it's wrong.
// This one would get called through code like
// if ( *mp1 == mp2 )
// or
// if ( mp1->operator==( mp2 ) )
// which is downright weird. How would your replacement ever understand code like that?
bool MyPointerBase::operator== (const MyPointerBase *other) const {
return (this->id == other->id);
}
To copy to clipboard, switch view to plain text mode
If you want a class operator==(), then you write this:
bool MyPointerBase::operator==( const MyPointerBase & other ) const
{
return id == other.id;
}
bool MyPointerBase::operator==( const MyPointerBase & other ) const
{
return id == other.id;
}
To copy to clipboard, switch view to plain text mode
and call it using pointers to class instances like this:
// In ALL cases mp1 and mp2 must be non-NULL
if ( *mp1 == *mp2 )
{
// ...
}
// or
if ( mp1->operator==( *mp2 ) )
{
// ...
}
// or
MyClass & mp1Ref = *mp1;
MyClass & mp2Ref = *mp2;
if ( mp1Ref == mp2Ref )
{
// ...
}
// In ALL cases mp1 and mp2 must be non-NULL
if ( *mp1 == *mp2 )
{
// ...
}
// or
if ( mp1->operator==( *mp2 ) )
{
// ...
}
// or
MyClass & mp1Ref = *mp1;
MyClass & mp2Ref = *mp2;
if ( mp1Ref == mp2Ref )
{
// ...
}
To copy to clipboard, switch view to plain text mode
Bookmarks