Instinct allways told me this was illegal, but now I think maybe I'm wrong:
As you can see, A CString object is created local to the function, manipulated, and then returned by value.
With most objects this would be completely illegal because the object would be distroyed at the end of the function, and you'd be returning basically a block of freed memory to the caller. Often the program still works because the freed memory still contains the contents you expect.
However, CString is reference counted. So does it handle this case correctly? MSDN Implies that it does, however the example they show has them returning a member variable, which is a completely different case (and how I've always done CString returns).
Please don't guess at whether it is legal. I'm looking for peice of mind.
code:<HR>Code:CString CSQLWrapper::CleanString(const CString &strSQL) { CString strReturn(strSQL); strReturn.Replace("'", "''"); return strReturn; }
As you can see, A CString object is created local to the function, manipulated, and then returned by value.
With most objects this would be completely illegal because the object would be distroyed at the end of the function, and you'd be returning basically a block of freed memory to the caller. Often the program still works because the freed memory still contains the contents you expect.
However, CString is reference counted. So does it handle this case correctly? MSDN Implies that it does, however the example they show has them returning a member variable, which is a completely different case (and how I've always done CString returns).
Please don't guess at whether it is legal. I'm looking for peice of mind.