Results 1 to 3 of 3

Thread: Convert overloaded member function into a member template function?

  1. #1
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Question Convert overloaded member function into a member template function?

    The code below of an overloaded member functions looks bloated:
    Qt Code:
    1. void MyClass::writeListToXml(const QList<int> &list, const QString &name)
    2. {
    3. foreach(int i, list) {
    4. xml_writer->writeTextElement(name, QString::number(i));
    5. }
    6. }
    7.  
    8. void MyClass::writeListToXml(const QList<double> &list, const QString &name)
    9. {
    10. foreach(double i, list) {
    11. xml_writer->writeTextElement(name, QString::number(i));
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    How could i pack them together?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    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: Convert overloaded member function into a member template function?

    Qt Code:
    1. template<typename T> void MyClass::writeListToXml(const QList<T> &list, const QString &name) {
    2. foreach(T i, list) {
    3. xml_writer->writeTextElement(name, QString::number(i));
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.


  3. #3
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Convert overloaded member function into a member template function?

    Magic!

    Thanks

Similar Threads

  1. Replies: 2
    Last Post: 23rd February 2012, 12:23
  2. to call member function
    By vinayaka in forum General Programming
    Replies: 5
    Last Post: 1st July 2011, 13:48
  3. QtConcurrent::run() with function member?
    By wookoon in forum Newbie
    Replies: 3
    Last Post: 8th July 2010, 13:12
  4. static member function
    By freekill in forum Newbie
    Replies: 4
    Last Post: 29th July 2009, 15:23
  5. Replies: 22
    Last Post: 8th October 2008, 13:54

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.