Re: How to pass a reference to a CComBSTR



>>> 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


.



Relevant Pages

  • Re: pointer/ref question
    ... reference" to generically indicate we're conveying the address of an ... to /me/ one can describe both of these situations as "passing ... the term "pass by pointer" for the first case when teaching C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: using const & in function prototypes
    ... BOOL func; ... You are passing a reference to an object instead of a copy. ... passing a pointer is at best as performant as passing a DWORD, ...
    (microsoft.public.vc.language)
  • Re: no pointer in Java => my problem
    ... > the value you are passing is an adress and you can't change the ... When passing a reference, you don't get ... access to the pointer. ... Neither Java nor C have anything resembling this. ...
    (comp.lang.java.programmer)
  • Re: pointer/ref question
    ... > reference" to generically indicate we're conveying the address of an ... > to have a separate term for that and passing to a function taking a ... > passing a pointer to x by value. ... I know I have the habit of saying pass by reference when a pointer is being ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to pass a reference to a CComBSTR
    ... > I'm passing in my CComBSTR like this: myfunction(& myCComBSTRvar) ... This is not passing by reference - you are passing pointers. ...
    (microsoft.public.vc.atl)