shaun wrote:
[...]
Would I be better off in returning by reference?:
vector<string> & DBXMLStructure::getFolderNames(){
const vector<string> & folderList(m_pdb->listFolders());
return folderList;
}
Definitely not. First of all, the compiler won't let you do that
without a const_cast -- you're trying to convert a "v<s> const&"
to "v<s>&". Second, if 'listFolders()' does return an r-value,
a temporary object, it only lives as long as 'folderList' reference.
Returning another reference initialised from 'folderList' will cause
undefined behaviour as soon as you try using it because it will be
invalid outside the function.
Also, if your 'DBXMLStructure' own 'm_pdb' object, it _might_ be OK
to return a reference to its (m_pdb's) data member, but only if the
'listFolders' function returns a reference itself. So, if that is
the case, and you rewrite your function as
vector<string> const& DBXMLStructure::getFolderNames() const
{
return m_pdb->listFolders();
}
it might be OK. (Note I made it 'const' as well).
V