BSTR 和 _bstr_t 有什么区别?

2022-01-14 00:00:00 com c++ smart-pointers

谁能解释上面提到的类型之间的区别以及一些示例用法以清楚地解释两者之间的区别?

Can anyone explain the difference between the types mentioned above and some sample usage to clearly explain the difference between the two?

任何帮助将不胜感激!注意:这个问题是 this other question

Any help would be highly appreciated! Note: this question is a spin-off from this other question

推荐答案

BSTR 是 COM 使用的字符串数据类型.

BSTR is the string data type used with COM.

_bstr_t 是一个像智能指针一样工作的包装类,因此当变量被销毁或超出范围时,它将释放分配的内存._bstr_t 还具有引用计数,每次按值传递 _bstr_t 变量时都会增加(避免不必要的复制),并在不再使用时减少.每当所有引用都被销毁时,为字符串分配的内存就会被释放.

_bstr_t is a wrapper class that works like a smart pointer, so it will free the allocated memory when the variable is destroyed or goes out of scope. _bstr_t also has reference counting, which increases every time you pass the _bstr_t variable by value (avoiding unnecessary copy) and decrement when it is no longer used. Whenever all references are destroyed, the allocated memory for the string is freed.

BSTR 的替代品是 CComBSTR.它还管理 BSTR 的内存,但没有引用计数.

An alternative to BSTR is the CComBSTR. It also manages the memory for the BSTR, but has no reference counting.

相关文章