B
Brian C
Hello all,
I have a question regarding syntax, coming from a C background. In C,
if I had a function like this:
char *ParseDelimitedString(char *String,int DelimitedNumber,char
*Retval,int MaxBytes)
{
....
return(Retval);
}
This would be the way to return a string, instead of trying to return a
local variable. Of course, you can malloc() within the function and
return that, but I dont like having the caller know he has to free() it,
but I digress..
In C++, if I had this function as a static function, in some StringUtil
class:
std::string &StringUtil:arseDelimitedString(string String,int
Num,string &Retval)
{
....
return(Retval);
}
or
std::string StringUtil:arseDelimitedString(string String,int Num)
{
string LocalVar;
return(LocalVar);
}
From testing, I did see that returning the LocalVar created a 3rd
string (did not call the constructor), and did copy the data into the
string class. So, I assume this works, but about creating a new class,
for performance reasons is my question.
1) Which is correct? 1, 2, both?
2) Which would be faster? I assume #1 because it is just returning a
reference.
Thanks all.
I have a question regarding syntax, coming from a C background. In C,
if I had a function like this:
char *ParseDelimitedString(char *String,int DelimitedNumber,char
*Retval,int MaxBytes)
{
....
return(Retval);
}
This would be the way to return a string, instead of trying to return a
local variable. Of course, you can malloc() within the function and
return that, but I dont like having the caller know he has to free() it,
but I digress..
In C++, if I had this function as a static function, in some StringUtil
class:
std::string &StringUtil:arseDelimitedString(string String,int
Num,string &Retval)
{
....
return(Retval);
}
or
std::string StringUtil:arseDelimitedString(string String,int Num)
{
string LocalVar;
return(LocalVar);
}
From testing, I did see that returning the LocalVar created a 3rd
string (did not call the constructor), and did copy the data into the
string class. So, I assume this works, but about creating a new class,
for performance reasons is my question.
1) Which is correct? 1, 2, both?
2) Which would be faster? I assume #1 because it is just returning a
reference.
Thanks all.