reference question

S

shaun

This is going to seem very naïve, I'm afraid:
I have the following:

vector<string> DBXMLStructure::getFolderNames(){
const vector<string> & folderList(m_pdb->listFolders());
return folderList;
}


My question:

when I return folderList, am I returning a reference to something
temporary which may cease to exist? Assuming that m_pdb->listFolders()
returns a vector by value (what if it doesnt?)

sheepish
shaun
 
V

Victor Bazarov

shaun said:
This is going to seem very naïve, I'm afraid:
I have the following:

vector<string> DBXMLStructure::getFolderNames(){
const vector<string> & folderList(m_pdb->listFolders());
return folderList;
}


My question:

when I return folderList, am I returning a reference to something
temporary which may cease to exist?

You're returning a whole new object of type 'vector<string>'. In order to
return that value, a copy of 'folderList' is going to be created. That
usually means every string in 'folderList' is copied (unless the 'vector'
is not the standard one but some other vector you have).
Assuming that m_pdb->listFolders()
returns a vector by value (what if it doesnt?)

What do you mean "what if it doesn't"? You wrote it to return the vector
by value, didn't you? It returns the vector by value. Why assume? Just
look and see.

V
 
S

shaun

Victor Bazarov said:
You're returning a whole new object of type 'vector<string>'. In order to
return that value, a copy of 'folderList' is going to be created. That
usually means every string in 'folderList' is copied (unless the 'vector'
is not the standard one but some other vector you have).


What do you mean "what if it doesn't"? You wrote it to return the vector
by value, didn't you? It returns the vector by value. Why assume? Just
look and see.

V

OK, thanks. I did not write the m_pdb->listFolders() function, although
my class does (now) own the m_pdb pointer. I will have to go to our CVS
repository and look to see whether the function returns by reference or
value, although I think I can safely assume that whoever wrote it is a
better programmer than I am.

Would I be better off in returning by reference?:

vector<string> & DBXMLStructure::getFolderNames(){
const vector<string> & folderList(m_pdb->listFolders());
return folderList;
}
 
V

Victor Bazarov

shaun said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top