Is it safe to pass a _bstr_t object to a function expecting BSTR as argument?

191 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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