Results 1 to 6 of 6

Thread: QList<mypointer *>::contains and operator== issue

  1. #1
    Join Date
    Jul 2009
    Location
    London
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QList<mypointer *>::contains and operator== issue

    Hi,

    I'm having some issues with QList<mypointer *>::contains and operator==.

    It is probably easiest to explain this with code - am basically attempting to compare a pointer with one in a list.

    Qt Code:
    1. QList <MyPointerBase *> p1;
    2. QList <MyPointerInherited *> p2; //already instantiated
    3.  
    4. for (int i = 0 ; i < p2.size() ; i++) {
    5. MyPointer *mp = new MyPointerBase(p2[i]);
    6. if (p1.isEmpty() || !p1.contains(mp))
    7. {
    8. p1 << mp;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    However the !p1.contains(mp) always returns true, I suppose because it is comparing two pointers?

    I have overloaded operator== for the two classes as well using both methods

    Surely the second one should be used for the comparison? I have dbugged this but the code never makes it here..

    Qt Code:
    1. bool operator== (MyPointerBase &pb1, MyPointerBase &pb2) {
    2. return (pb1.id == pb2.id);
    3. }
    4.  
    5. bool MyPointerBase::operator== (const MyPointerBase *other) const {
    6. return (this->id == other->id);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Aside from creating my own version of "contains" can anyone see anything obvious I am doing wrong?

    Thanks

    :-)

  2. #2
    Join Date
    Jan 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QList<mypointer *>::contains and operator== issue

    If I not mistaken, bool MyPointerBase:perator== (const MyPointerBase *other) const { is never called because you compare pointers.

    edit : and operator== takes (or should take) only references. wiki link
    Last edited by minirop; 1st May 2011 at 02:43.

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QList<mypointer *>::contains and operator== issue

    What a mess you've got there
    If I understood it correctly, you want to check all elements from p2 and if they are not already in p1, you want to append them ?

    Qt Code:
    1. for(int i=0;i<p2.size();i++)
    2. {
    3. if (!p1.contains(p2.at(i) )
    4. {
    5. MyPointerBase *mp = new MyPointerBase ();
    6. *mp=*p2.at(i);
    7. p1.append(mp);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have dbugged this but the code never makes it here..
    Because in your code you not using the operator == (at least in the code you show).
    I have not tested my code, dont now if it will work
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  4. #4
    Join Date
    Jul 2009
    Location
    London
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList<mypointer *>::contains and operator== issue

    Hey,

    Thanks. Yes it is a bit ugly but the code is more complicated than above.. The classes have unique id's and so the == overload just checked that rather than everything in the class.

    I found another way to do it that was a bit cleaner (well I hope) by iterating over a QMap of these id's.

    Thanks though!

    :-)

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList<mypointer *>::contains and operator== issue

    But do you realize what was wrong with your original code?

    Qt Code:
    1. 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.

    Qt Code:
    1. 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.

    Qt Code:
    1. // This really isn't a class member at all.
    2. // This operator would be invoked through a call like
    3. // if ( *mp1 == *mp2 )
    4. bool operator== (MyPointerBase &pb1, MyPointerBase &pb2) {
    5. return (pb1.id == pb2.id);
    6. }
    7.  
    8. // This one is a class member, but it's wrong.
    9. // This one would get called through code like
    10. // if ( *mp1 == mp2 )
    11. // or
    12. // if ( mp1->operator==( mp2 ) )
    13. // which is downright weird. How would your replacement ever understand code like that?
    14.  
    15. bool MyPointerBase::operator== (const MyPointerBase *other) const {
    16. return (this->id == other->id);
    17. }
    To copy to clipboard, switch view to plain text mode 

    If you want a class operator==(), then you write this:

    Qt Code:
    1. bool MyPointerBase::operator==( const MyPointerBase & other ) const
    2. {
    3. return id == other.id;
    4. }
    To copy to clipboard, switch view to plain text mode 

    and call it using pointers to class instances like this:

    Qt Code:
    1. // In ALL cases mp1 and mp2 must be non-NULL
    2. if ( *mp1 == *mp2 )
    3. {
    4. // ...
    5. }
    6. // or
    7.  
    8. if ( mp1->operator==( *mp2 ) )
    9. {
    10. // ...
    11. }
    12.  
    13. // or
    14.  
    15. MyClass & mp1Ref = *mp1;
    16. MyClass & mp2Ref = *mp2;
    17. if ( mp1Ref == mp2Ref )
    18. {
    19. // ...
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2009
    Location
    London
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList<mypointer *>::contains and operator== issue

    Perfect. Thanks for the pointers.. Finally got it to work!

    I think I understand it now - definitely now it isn't 1am!

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  3. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  4. Replies: 3
    Last Post: 25th February 2009, 16:55
  5. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.