Results 1 to 5 of 5

Thread: references or values?

  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default references or values?

    Hi,
    a trivial Question to you:

    What's better style:
    this
    Qt Code:
    1. class Test{
    2. private:
    3. QString one;
    4. QString two;
    5.  
    6. public:
    7. Text(const QString& one,const QString& two);
    8. }
    9.  
    10. //cpp
    11.  
    12. Test::Test(const QString& one,const QString& two) :
    13. one(one),
    14. two(two)
    15. {
    16. }
    To copy to clipboard, switch view to plain text mode 
    or that:
    Qt Code:
    1. class Test{
    2. private:
    3. QString one;
    4. QString two;
    5.  
    6. public:
    7. Text(QString one, QString two);
    8. }
    9.  
    10. //cpp
    11.  
    12. Test::Test(QString one,QString two) :
    13. one(one),
    14. two(two)
    15. {
    16. }
    To copy to clipboard, switch view to plain text mode 

    and what's are the advantages and disatvantages?

    I prefer the first option, because it looks like more performance.. Am I right?

    thanks
    Last edited by Qiieha; 24th April 2013 at 10:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: references or values?

    I would use the first one because it fits better into the overall coding style used by Qt.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Qiieha (24th April 2013)

  4. #3
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: references or values?

    The first one is actually more like general C++ coding style, second is used by Qt.

    You can read about it here: http://qt-project.org/doc/qt-4.8/implicit-sharing.html

  5. The following user says thank you to RSX for this useful post:

    Qiieha (26th April 2013)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: references or values?

    I would use both because they are not really comparable. Will say: I would use
    Qt Code:
    1. const QString &one
    To copy to clipboard, switch view to plain text mode 
    and not
    Qt Code:
    1. const QString one
    To copy to clipboard, switch view to plain text mode 
    if they should not be changeable, but I would use
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    and not
    Qt Code:
    1. QString &one
    To copy to clipboard, switch view to plain text mode 
    if they should be changeable. (Because Implicit Sharing is cheap.)

  7. The following user says thank you to Lykurg for this useful post:

    Qiieha (26th April 2013)

  8. #5
    Join Date
    Apr 2011
    Posts
    195
    Thanks
    49
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: references or values?

    thank u guys.
    this is very good to know =)

Similar Threads

  1. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  2. undefined references to mysql
    By Ahas in forum Installation and Deployment
    Replies: 3
    Last Post: 25th May 2011, 18:02
  3. Question about references in C++
    By jano_alex_es in forum General Programming
    Replies: 10
    Last Post: 18th January 2011, 12:19
  4. Pointers and references
    By PaceyIV in forum Newbie
    Replies: 10
    Last Post: 27th July 2009, 10:53
  5. QWebView undefined references
    By xtreme in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2008, 08:08

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.