Re: How to pass a reference to a CComBSTR
- From: "Steve Franks" <please@xxxxxxxxxxxxxxxxx>
- Date: Sun, 31 Jul 2005 11:25:52 -0400
>>> I'm passing in my CComBSTR like this: myfunction(& myCComBSTRvar)
>>> and myfunction looks like this:
>>> void myfunction(CComBSTR *myCComBSTRvarToUpdate) {}
>>
>> This is not passing by reference - you are passing pointers. To pass by
>> reference yous should declare myfunction as
>>
>> void myfunction(CComBSTR& myModifiableVariable)...
>>
>> and call it like
>>
>> myfunction(myCComBSTRvar);
>>
Very interesting. Is there any performance difference (or any practical
difference for that matter) between passing pointers vs. passing by
reference?
It looks like I have two approaches that both work. I am wondering which
one is better (faster performing and safer):
Approach A (passing by reference)
define function to pass by reference:
void myfunction(CComBSTR& myModifiableVariable)
and call like this:
myfunction(myCComBSTRvar);
and set the "return" (out) result like this at the end of myfunction:
CComBSTR tempCComBSTR(mySTLString.c_str());
myModifiableVariable = tempCComBSTR.Detach();
Approach B (passing by pointer)
define function to pass by pointer:
void myfunction(BSTR* myModifiableVariable) //cannot use CComBSTR in the
declaration but BSTR works
and call like this:
myfunction( & myCComBSTRvar);
and set the "return" (out) result like this at the end of myfunction:
CComBSTR tempCComBSTR(mySTLString.c_str());
*myModifiableVariable = tempCComBSTR.Detach();
Both approaches appear to work equally as well. Does one work better than
the other? Any performance difference? I suppose in this case passing by
reference is clearner because of the odd issue with CComBSTR not returning a
pointer and having to use BSTR.
On a related note, is it perfectly safe to pass a pointer to a CComBSTR
around as a BSTR, like shown in Approach B?
One final question if you will. What is the best way to convert a string
contained in an STL "string" variable type to a CComBSTR? For example,
currently I do this:
string mySTLString = "Hello";
CComBSTR tempCComBSTR(mySTLString.c_str());
myModifiableVariable = tempCComBSTR.Detach();
Is that an efficient way, or is there a better way?
Thanks again. You all have been a great help and I really appreciate it.
Steve
.
- Follow-Ups:
- Re: How to pass a reference to a CComBSTR
- From: Steve Franks
- Re: How to pass a reference to a CComBSTR
- From: Heinz Ozwirk
- Re: How to pass a reference to a CComBSTR
- References:
- How to pass a reference to a CComBSTR
- From: Steve Franks
- Re: How to pass a reference to a CComBSTR
- From: Heinz Ozwirk
- Re: How to pass a reference to a CComBSTR
- From: Egbert Nierop \(MVP for IIS\)
- How to pass a reference to a CComBSTR
- Prev by Date: Re: How to pass a reference to a CComBSTR
- Next by Date: Re: How to pass a reference to a CComBSTR
- Previous by thread: Re: How to pass a reference to a CComBSTR
- Next by thread: Re: How to pass a reference to a CComBSTR
- Index(es):
Relevant Pages
|