Results 1 to 13 of 13

Thread: Simple: Operator overloading with heap objects

  1. #1
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    84
    Thanks
    5
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Simple: Operator overloading with heap objects

    Okay, it's seems like I'm missing something here:

    Qt Code:
    1. #include <QString>
    2. #include <iostream>
    3.  
    4. class TestItem
    5. {
    6. public:
    7. TestItem(const QString &value)
    8. {
    9. m_value = value;
    10. }
    11.  
    12. QString value() const
    13. {
    14. return m_value;
    15. }
    16.  
    17. bool operator==(const TestItem &other) const
    18. {
    19. return this->value() == other.value();
    20. }
    21.  
    22. private:
    23. QString m_value;
    24. };
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. TestItem *one = new TestItem("abc");
    29. TestItem *two = new TestItem("abc");
    30.  
    31. if(one == two)
    32. std::cout << "Match";
    33. else
    34. std::cout << "No Match";
    35.  
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 
    This returns ALWAYS "No Match". But if I create the objects on the stack and not on the heap it works:
    Qt Code:
    1. TestItem one("abc");
    2. TestItem two("abc");
    To copy to clipboard, switch view to plain text mode 
    What do I have to do to make it work with heap initialized objects ? Thank you.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simple: Operator overloading with heap objects

    You are comparing pointers instead of the objects itself, try:
    Qt Code:
    1. if (*one == *two)
    2. ...
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    84
    Thanks
    5
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Simple: Operator overloading with heap objects

    Yeah, I know, thanks
    I just thought there might be a way to do it without the stars?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple: Operator overloading with heap objects

    This should work:
    Qt Code:
    1. bool operator==(const TestItem *one, const TestItem *two) const {
    2. return one->value() == two->value();
    3. }
    To copy to clipboard, switch view to plain text mode 

    But this is risky, because you might have trouble checking for null values.

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Quote Originally Posted by wysota View Post
    This should work:
    Qt Code:
    1. bool operator==(const TestItem *one, const TestItem *two) const {
    2. return one->value() == two->value();
    3. }
    To copy to clipboard, switch view to plain text mode 
    But this is risky, because you might have trouble checking for null values.
    This one is probably safer, though a little slower...
    Qt Code:
    1. bool operator==(const TestItem *one, const TestItem *two) const {
    2. return (one && two) ? (one->value() == two->value()) : false;
    3. }
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Sure nothing is stopping you from doing this.

    But you probably wouldn't want to do this kind of overloading as you are giving a new meaning to the == operator on pointers. Many a times the users of your code would not think that you are comparing the values.
    We can't solve problems by using the same kind of thinking we used when we created them

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Quote Originally Posted by sunil.thaha View Post
    But you probably wouldn't want to do this kind of overloading as you are giving a new meaning to the == operator on pointers. Many a times the users of your code would not think that you are comparing the values.
    That's why it's impossible in C++

  8. #8
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    That's why it's impossible in C++
    You are absolutely right. And thank you spotting out our mistake. I will never forget this

    BTW: did you try out what was said here. Or you got it at the first go ?
    We can't solve problems by using the same kind of thinking we used when we created them

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Quote Originally Posted by sunil.thaha View Post
    did you try out what was said here. Or you got it at the first go ?
    I knew that it's impossible, but I've checked it just to be sure.

  10. #10
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Quote Originally Posted by jacek View Post
    I knew that it's impossible, but I've checked it just to be sure.
    Just curious : what error was thrown by the compiler??? (don't tell me "check it on your own" because I just can't ATM...)
    Current Qt projects : QCodeEdit, RotiDeCode

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Quote Originally Posted by fullmetalcoder View Post
    Just curious : what error was thrown by the compiler???
    Something like: "at least one of operator arguments must be a class or an enum".

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Simple: Operator overloading with heap objects

    I wonder if the behaviour is simmilar for all popular compilers.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple: Operator overloading with heap objects

    Quote Originally Posted by wysota View Post
    I wonder if the behaviour is simmilar for all popular compilers.
    It should be, at least if they are C++ compilers:
    An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

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.