Re: question about CComBSTR / _bstr_t subtlety
Jason S <jmsachs@gmail.com> wrote:
I have a head-scratcher question about how CComBSTR / _bstr_t work in
a certain case.
Let's say I have these functions:
_bstr_t f()
{
_bstr_t b;
b = L"foo";
b += L"bar";
return b;
}
void g(BSTR b0)
{
// do something with b0
}
void h()
{
g(f());
}
If I call h(), how many times does the string "foobar" get copied
after its initial construction inside f()?
_bstr_t is reference-counted. The underlying BSTR is not copied by the
copy-constructor.
I guess I am confused
about how functions can return _bstr_t or CComBSTR as values w/o
causing a copy of the underlying string to be made.
CComBSTR does make a copy of the underlying string. What makes you
believe it doesn't?
A function can take _bstr_t& or CComBSTR& parameter by reference, and
place its output into the caller-supplied variable, if you are worried
about performance. As usual, before engaging in microoptimizations,
profile to make sure they do in fact contribute to performance
bottlenecks.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925