C++ BSTR 和 _bstr_t 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/341462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between BSTR and _bstr_t?
提问by JohnIdol
Can anyone explain the difference between the types mentioned above and some sample usage to clearly explain the difference between the two?
谁能解释一下上面提到的类型和一些示例用法之间的区别,以清楚地解释两者之间的区别?
Any help would be highly appreciated! Note: this question is a spin-off from this other question
任何帮助将不胜感激!注意:这个问题是从另一个问题衍生出来的
回答by Khalid Salom?o
BSTR is the string data type used with COM.
BSTR 是与 COM 一起使用的字符串数据类型。
_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_t 是一个包装类,其工作方式类似于智能指针,因此当变量被销毁或超出范围时,它将释放分配的内存。_bstr_t 也有引用计数,每次按值传递 _bstr_t 变量时都会增加(避免不必要的复制),并在不再使用时减少。每当所有引用都被销毁时,为字符串分配的内存将被释放。
An alternative to BSTR is the CComBSTR. It also manages the memory for the BSTR, but has no reference counting.
BSTR 的替代方案是 CComBSTR。它还管理 BSTR 的内存,但没有引用计数。
回答by efotinis
BSTRis a raw pointer, while _bstr_t
is a class encapsulating that pointer.
BSTR是一个原始指针,_bstr_t
而是一个封装该指针的类。
It's the same difference as char*vs. std::string.
它与char*与std::string 的区别相同。
回答by PiNoYBoY82
_bstr_t wraps the BSTR type. So, when you instantiate a _bstr_t, you are also creating BSTR. _bstr_t simply wraps everything up for you and acts sort of like a "smart ptr" to the BSTR.
_bstr_t 包装 BSTR 类型。因此,当您实例化 _bstr_t 时,您也在创建 BSTR。_bstr_t 简单地为您包装了所有内容,并且有点像 BSTR 的“智能 ptr”。
BSTR
BSTR
http://msdn.microsoft.com/en-us/library/ms221069.aspx
http://msdn.microsoft.com/en-us/library/ms221069.aspx
SysAllocString()
SysAllocString()
http://msdn.microsoft.com/en-us/library/ms891285.aspx
http://msdn.microsoft.com/en-us/library/ms891285.aspx
_bstr_t
_bstr_t