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.