By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

reference question

P: n/a
This is going to seem very nave, 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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
shaun wrote:
This is going to seem very nave, 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
Jul 23 '05 #2

P: n/a
In article <Er*******************@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.********@comAcast.net> wrote:
shaun wrote:
This is going to seem very nave, 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


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;
}
Jul 23 '05 #3

P: n/a
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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.