Results 1 to 2 of 2

Thread: Overloading + operator in QString

  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Overloading + operator in QString

    I want to overload the + operator for my mainwindow class(or any other class) for Qstring. this is what I have done so far :
    Qt Code:
    1. QString operator+(QString a,QString b)
    2. {
    3. qDebug()<<"works";
    4. }
    To copy to clipboard, switch view to plain text mode 
    but the thing is, the QString + operator is already overloaded(to concatenate, I guess). so, if I use the above code, it results in ambiguity(both the signatures are same). how do I override the actual function to my own function without making a new class to hold QString?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Overloading + operator in QString

    Why ? What do you expect to get with for example "x" + "y" other than "xy" ?
    If you need custom behaviour just use custom methods, like
    Qt Code:
    1. QString myPlus(const QString& a, const QString& b)
    To copy to clipboard, switch view to plain text mode 
    .
    You cannot provide new declarations for functions / classes etc. that are already defined.
    If you really want, you can implement the operator+ as a member method of your class, like:
    Qt Code:
    1. class Test{
    2. public:
    3. Test(const QString& s){
    4. buff = s;
    5. }
    6. QString operator+(const QString& s){
    7. return buff+s;
    8. }
    9. private:
    10. QString buff;
    11. };
    12.  
    13. int main(int argc, char ** argv)
    14. {
    15. Test t("x");
    16. qDebug() << t+"y";
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 
    or as non-member function and make it a "friend" of the class...
    I don't know, personally I just avoid overloading operators.

    Btw. It is never a good idea to modify something that is well defined. This is probably your personal project, but imagine that someone will someday work on this code, he'd be very surprised to find that "newString = string1+string2" is not really what its supposed to be, or have a nasty side effects...

Similar Threads

  1. Replies: 6
    Last Post: 11th March 2012, 16:37
  2. Overloading QMap << operator
    By The 11th plague of Egypt in forum Newbie
    Replies: 3
    Last Post: 14th September 2011, 19:24
  3. Operator Overloading
    By naturalpsychic in forum Newbie
    Replies: 1
    Last Post: 19th July 2011, 05:19
  4. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28
  5. operator [] overloading
    By darksaga in forum General Programming
    Replies: 5
    Last Post: 8th April 2008, 15:27

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
  •  
Qt is a trademark of The Qt Company.