473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

reference question

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
Jul 23 '05 #1
3 1599
shaun wrote:
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
Jul 23 '05 #2
In article <Er*******************@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.********@comAcast.net> wrote:
shaun wrote:
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
1
by: Michel Couche | last post by:
Hello, I am starting the development of a newsletter The use of the class MailMessage of System.Net.Mail is quite straightforward for usual contact forms but my question here is "How can I...
3
by: pleaseexplaintome_2 | last post by:
using the code below (some parts not included), I create a new excel workbook with spreadheets. I then want to delete a spreadsheet, but a reference remains open and excel stays in task manager...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.