1

I am new to C++ so please bear with me. I know that _bstr_t is just a wrapper class for BSTR but, as seen in the MSDN documentation, _bstr_t does not have an operator to convert itself to BSTR.

So can I pass a _bstr_t object to a function expecting a BSTR as argument, and is it safe?

Or is there any better way to do this? I especially don't want any memory leaks with this.

I have seen numerous articles so just got confused about this. Sorry if it is a trivial question.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

0

_bstr_t does not have an operator to convert itself to BSTR

Yes and no. BSTR is defined to be equivalent to wchar_t* in the "afx.h" header file:

typedef _Null_terminated_ LPWSTR BSTR;

And the _bstr_t class (in "comutil.h") does have conversion operators for that, with or without a const qualifier, as in:

class _bstr_t {
public:
//...
//...
    // Extractors
    //
    operator const wchar_t*() const throw();
    operator wchar_t*() const throw();
//...

EDIT: But one minor 'note of caution' from the documentation for those operators:

Assigning a new value to the returned pointer does not modify the original BSTR data.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • So would it be safe for me to pass the _bstr_t object as parameter and not worry about any leaks or issues?? – Raja Shekar Dec 09 '20 at 08:10
  • 1
    @RajaShekar Yes! If the function parameter should be a `BSTR` then the conversion will be done; and I would like to think the *implementations* of the two operators whose *declarations* I have shown would be 'safe'. – Adrian Mole Dec 09 '20 at 08:12