Return string from function?

Started by
1 comment, last by PyroBlizzard 18 years, 10 months ago
Ok, so I have a function (obviously) that gets a registry value, and stores it to a string value. Is there any way I can return this value from my function?
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
Advertisement
I'm assuming you're using C++. If not, let us know which language you are using. If C++, though, the preferred method would be to use std::string, found in <string>. It doesn't have any of the problems of the C-style string (char*) would have in this situation. The character data will actually get copied automatically for you, and all dynamically allocated memory will be freed without you having to worry about it.
#include <string>std::string ReturnAString(){  std::string s;  //stuff  return s;}

If you ever need to read from a std::string as though it were a character array, use the function s.c_str(). This will return a const char*. If you need to write into it as though it's a character array, then you'll likely just have to create a temporary character array, read the data into that, and then set the string variable equal to the character array; all the data will get copied into the string object.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Thanks for the help, but I figured it out right before I saw that. I am using managed C++, so for the reference of anybody else who might have the problem, I had to do such

Quote:System::String^ functionName()
{
//do stuff
return stringName;
}
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?

This topic is closed to new replies.

Advertisement