Results 1 to 3 of 3

Thread: Returning const &

  1. #1
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Returning const &

    In my app I have I class that handles file IO. I have a protected method that contains the code for reading from a file. Basically every method that reads from a file makes some preparations and that calls this protected method.

    Qt Code:
    1. QStringList WODAL::readFile(const QString &name)
    2. {
    3. QStringList result;
    4. QFile file(name);
    5. QTextStream stream;
    6.  
    7. if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    8. {
    9. stream.setDevice(&file);
    10. while(!stream.atEnd())
    11. {
    12. result.append(stream.readLine());
    13. }
    14. }
    15.  
    16. file.close();
    17.  
    18. return result;
    19. }
    To copy to clipboard, switch view to plain text mode 

    When I change the return type from QStringList to const QStringList& I get the compiler warning returning of the reference to a local variable, or something like that. Since I have another class which is mostly a property container. It has lot of members that only have setters and getters I wanted to make a getter that is for instance like this

    Qt Code:
    1. const QString& WOIDL::clientName()
    2. {
    3. return m_clientName; //m_clientName is of QString type
    4. }
    To copy to clipboard, switch view to plain text mode 

    For this getter I don't get the warning because m_clientName is not a local variable from some method which will be destroyed upon returning but a class member. My question is: Is it ok to use const QString& and const QStringList& in these cases or not? Its been a while since I read on these topics and I am not sure when is return variable destroyed. For instance will this code be ok if I write it in some method of the WODAL class in case return type is const QStringList&?

    Qt Code:
    1. QString name("somefile");
    2.  
    3. list = readFile(name);
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. const QStringList& WODAL::loadClientList()
    2. {
    3. return readFile(m_clientListFileName); //m_clientList is of QString type and was previously initialized
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advace.

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Returning const &

    When returning a reference, as a quick check on your logic, you can think, “What would happen if it were a pointer?” Could you return a pointer to result? Of course not, because once the member function ends, its local variables, including result, won’t exist anymore.

    Fortunately, there is no need to get into this trap with QString and QStringList, because they are implicitly shared. You can pass implicitly shared classes as arguments and return them from functions as values (that is, not using pointers or references) without incurring the overhead of copying data; so there is usually no advantage to using const ...& with these classes.

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

    frenk_castle (24th November 2009)

  4. #3
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Returning const &

    Implicit sharing was one of the reasons I asked the question. I was 90% sure that returning const& is bad because that variable is on stack. One you return from function that stack memory is marked as free and something will overwrite the variable whose reference I returned. Thanks for the quick reply.

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 01:09
  2. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  3. shared vs static
    By alisami in forum Installation and Deployment
    Replies: 3
    Last Post: 4th October 2008, 13:04
  4. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.