Results 1 to 4 of 4

Thread: Overloading QMap << operator

  1. #1

    Default Overloading QMap << operator

    I don't remember very well how to overload operators in a template, so I humbly hope somebody can help me with this.

    I'd like to do something like this
    Qt Code:
    1. Contact contact("Name", "Surname");
    2. QMap<QString, Contact> map;
    3. map.insert("first", contact);
    4. qDebug() << map;
    To copy to clipboard, switch view to plain text mode 
    and have an output like this
    Qt Code:
    1. first: Name Surname
    To copy to clipboard, switch view to plain text mode 
    This is a just a basic example to help me understand templates, I know there are simpler ways, like a for loop.
    BTW is it possible to have a different overloading of << for QMap<QString, Contact> and QMap<Contact, QString> ?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,710
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Overloading QMap << operator

    I don't remember very well how to overload operators in a template
    Ok.
    So post the overloading for a regular class then, and we'll go from there.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3

    Default Re: Overloading QMap << operator

    Hum, I know that the best way to overload the << operator is using an external function.
    Qt Code:
    1. ostream& operator<<(ostream &os, const myClass &mc)
    2. {
    3. return os << myClass.whatever;
    4. }
    To copy to clipboard, switch view to plain text mode 

    And declare that function as a friend of the class
    Qt Code:
    1. Class myClass
    2. {
    3. friend ostream& operator<<(ostream &os, const myClass &mc);
    4. //etc
    5. };
    To copy to clipboard, switch view to plain text mode 

    I know templates are a little different though, and I don't know if I can use qDebug() just like an STL ostream.

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

    Default Re: Overloading QMap << operator

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Operator Overloading
    By naturalpsychic in forum Newbie
    Replies: 1
    Last Post: 19th July 2011, 05:19
  2. SOLVED: Operator overloading QDataStream
    By eekhoorn12 in forum Qt Programming
    Replies: 1
    Last Post: 9th May 2010, 23:55
  3. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28
  4. operator [] overloading
    By darksaga in forum General Programming
    Replies: 5
    Last Post: 8th April 2008, 15:27
  5. Simple: Operator overloading with heap objects
    By durbrak in forum General Programming
    Replies: 12
    Last Post: 25th April 2007, 13:20

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.