Hello, I have just implemented a few functions in a class of my program where some of them should return a std::string.
In those functions I use a temporary string to get the desired data and I return it at the end of the function. However I never get the string when I call the function, it seems to be destroyed before he returns it.
But I thought that string stays alive when it gets returned and is not being destroyed when he quits the function (or at least a copy is made)? Also I found some examples where this should work (function with a temporary string that is being returned).
Here's one of the mentioned functions I wrote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
string Config::GetVar(char* varname)
int start, end;
string result = "";
string searchString = varname;
start = var_buffer.find(searchString); //var_buffer is a class variable of type string
end = var_buffer.find_first_of('\n', start);
if(start != string::npos && end != string::npos)
int actualStart = start+searchString.size();
result = var_buffer.substr(actualStart, (end-actualStart));
So now my questions are, why doesn't this work and what do I need to do to make it work (as efficient as possible)?
Using your code but adding a class declaration and a main() with some test code as below, the result variable in Config::GetVar(...) ends up with "100" and so does the local variable named 'result' in the main():
I might add that I have a pointer to Config Config* c = new Config() and that previously I didn't actually wanted to get the string from the function but its const char*.
I originally did constchar* as return type and called return result.c_str(); but the result was the same as it is now so I changed it to string.
So my function calling looks like this: constchar* value = c->GetVar("variable2").c_str();
And when I do it like this, value contains an empty string.
If I do this however: string value = c->GetVar("variable2");
then value contains the wanted value.
But either way, shouldn't the first method also work or am I missing something here?
If possible I would like to use the first method because I don't always want to create a new string and call it with .c_str() (which I would have to do a couple of times in the code).
c_str() just returns a pointer to the internal array of the std::string. If you store the pointer that is returned from c_str() you will have to be careful. If the std::string need to reallocate it's internal array or the string is destroyed the pointer will no longer be valid.
Well if you are trying to return the address of a local variable to your function, obviously this won't work as by the time the caller gets the returned result, the local variable in your function to which you are returning the address will have gone out of scope.
If you want to return an address you can either declare the result of the function in the argument list, so the variable will have been declared by the caller and passed the address through to the function. Or you could use a temporary member variable in your class. Here are both methods: