M
Mike Cain
Hi - I am looking for the most efficient way to pass a STL string from one
function to another (using MS VS 7.0 ATL if that matters) and have a few
questions abuot the principles at work here.
Typically I have a helper function that does common things like replace all
occurances of a STL string in a given STL string. These strings can be
quite long in lenght, and therefore I do not want to pass the string by
VALUE because of all the memory allocations etc.
So this is how I currently and pass strings around:
APPROACH 1:
string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";
// replace all occurances of foo with bar
MyUtil::replaceAll(&strMyStr, &valToFind, &strReplaceWith);
void MyUtil::replaceAll(string* strMyStr, string* valToFind, string*
strReplaceWith)
{
// reference string values like this: *strMyStr
// call string methods like this: strMyStr->length();
// do replacement, replacing *strMyStr directly with modified
... code goes here, nothing returned
}
APPROACH 2:
string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";
// replace all occurances of foo with bar
MyUtil::replaceAll(strMyStr, valToFind, strReplaceWith);
void MyUtil::replaceAll(string& strMyStr, string& valToFind, string&
strReplaceWith)
{
// reference string values like this: strMyStr (no preceeding * needed
as in approach 1)
// call string methods like this: strMyStr.length();
// do replacement, replacing strMyStr directly with modified
... code goes here, nothing returned
}
Can someone please let me know if approach 1 and 2 are identical in terms of
performance and function? Does approach 2 pass by REFERENCE? If so I much
prefer it over approach 1 because of its simplier and cleaner notiation of
just refering to the var without * and ->.
The only downside with either of these techniques is that it makes for
awkward programming syntax because the target variable must be defined ahead
of time. For instance you have to do this with approach 1 or 2:
string myTargetToHoldVarSetInFunction;
somefunction(&myTargetToHoldVarSetInFunction, &someStrParam1,
&someStrParam2);
Whereas I prefer this:
string myTargetToHoldVarSetInFunction = somefunction(&someStrParam1,
&someStrParam2);
Am I correct, however, with the approach used in that last line of code the
value from the function must be returned by VALUE and therefore a large
string has to be recreated as the function exists and returns a value to the
caller? Any way to use that notation and return by REFERENCE?
Thanks!
function to another (using MS VS 7.0 ATL if that matters) and have a few
questions abuot the principles at work here.
Typically I have a helper function that does common things like replace all
occurances of a STL string in a given STL string. These strings can be
quite long in lenght, and therefore I do not want to pass the string by
VALUE because of all the memory allocations etc.
So this is how I currently and pass strings around:
APPROACH 1:
string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";
// replace all occurances of foo with bar
MyUtil::replaceAll(&strMyStr, &valToFind, &strReplaceWith);
void MyUtil::replaceAll(string* strMyStr, string* valToFind, string*
strReplaceWith)
{
// reference string values like this: *strMyStr
// call string methods like this: strMyStr->length();
// do replacement, replacing *strMyStr directly with modified
... code goes here, nothing returned
}
APPROACH 2:
string strMyStr = "whatever foo something foo whatever";
string valToFind = "foo";
string strReplaceWith = "bar";
// replace all occurances of foo with bar
MyUtil::replaceAll(strMyStr, valToFind, strReplaceWith);
void MyUtil::replaceAll(string& strMyStr, string& valToFind, string&
strReplaceWith)
{
// reference string values like this: strMyStr (no preceeding * needed
as in approach 1)
// call string methods like this: strMyStr.length();
// do replacement, replacing strMyStr directly with modified
... code goes here, nothing returned
}
Can someone please let me know if approach 1 and 2 are identical in terms of
performance and function? Does approach 2 pass by REFERENCE? If so I much
prefer it over approach 1 because of its simplier and cleaner notiation of
just refering to the var without * and ->.
The only downside with either of these techniques is that it makes for
awkward programming syntax because the target variable must be defined ahead
of time. For instance you have to do this with approach 1 or 2:
string myTargetToHoldVarSetInFunction;
somefunction(&myTargetToHoldVarSetInFunction, &someStrParam1,
&someStrParam2);
Whereas I prefer this:
string myTargetToHoldVarSetInFunction = somefunction(&someStrParam1,
&someStrParam2);
Am I correct, however, with the approach used in that last line of code the
value from the function must be returned by VALUE and therefore a large
string has to be recreated as the function exists and returns a value to the
caller? Any way to use that notation and return by REFERENCE?
Thanks!