Results 1 to 10 of 10

Thread: Disturbing: const QString &

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    43
    Thanked 21 Times in 21 Posts

    Default Re: Disturbing: const QString &

    Ok, I see what you are saying. Now I see the danger of passing things by reference (even if declared const). The advantage is that the parameter does not need to be copied, but the tradeoff is that you need to worry about whether the variable is changed part way through the execution of the function - not within the function but elsewhere.

    This is probably what was happening to me. But I will never know, because I've restructured my program to avoid the recursion (I'm now using a queuing system to do the same task).

    Based on a recent recommendation on this forum I changed my coding habits and use "const QString &" everywhere instead of "QString".... but now I see that I need to be more careful. Do you have a recommendation of what precautions I need to take with this?

    Thanks!



    Quote Originally Posted by jacek View Post
    This means that we have to take a look at this completely different function. It isn't time for worrying yet.

    Probably your code does something similar to this:
    Qt Code:
    1. #include <iostream>
    2.  
    3. int z = 0;
    4.  
    5. void bar( int y )
    6. {
    7. z = y;
    8. }
    9.  
    10. void foo( const int & x )
    11. {
    12. std::cerr << x << std::endl;
    13. bar( 1 );
    14. std::cerr << x << std::endl;
    15. }
    16.  
    17. int main()
    18. {
    19. foo( z );
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to magland for this useful post:

    Gopala Krishna (20th November 2007)

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

    Default Re: Disturbing: const QString &

    Quote Originally Posted by magland View Post
    Based on a recent recommendation on this forum I changed my coding habits and use "const QString &" everywhere instead of "QString".... but now I see that I need to be more careful.
    It's recommended to pass objects by reference to avoid copying, but on the other hand in Qt it's cheap to copy implicitly shared classes.

    Quote Originally Posted by magland View Post
    Do you have a recommendation of what precautions I need to take with this?
    I was thinking about this for few days and I couldn't come up with any rules. You situation is a bit similar to:
    Qt Code:
    1. ...
    2. v.append( v[0] );
    To copy to clipboard, switch view to plain text mode 
    The argument is passed as reference, but it must be copied when QVector needs to reallocate data. You can easily identify such situation in a single data structure, but the problem arises when the data is passed between many classes. But I would simply classify that as one of those Bad Things(tm) that might happen when you use global variables (or you have cycles in call graph).

  4. #3
    Join Date
    Feb 2007
    Location
    Philadelphia, USA
    Posts
    255
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    43
    Thanked 21 Times in 21 Posts

    Default Re: Disturbing: const QString &

    Thanks Jacek. I agree with you.

    After finding a number of subtle problems caused by passing by reference (in addition to the one I reported) I have concluded that it is important to NEVER pass arguments by reference UNLESS you can guarantee that the function does not access global variables, or "have cycles in the call graph" that may end up modifying the input variable. It is just not worth the danger! There's nothing worse than problems that occur mysteriously once in every 1000 calls.

    Of course, passing by reference is good, if you can ensure such things. I don't use global variables, but I do use a model/view framework which inevitably will have some cycles or recursive calls. Therefore, I need to be careful about it.

Similar Threads

  1. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 08:43
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42
  4. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 20:47
  5. Replies: 4
    Last Post: 24th March 2006, 23:50

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.