【发布时间】:2015-01-25 00:53:01
【问题描述】:
重现问题的最少代码:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
CComBSTR ccbTest( L"foo" );
const wchar_t * pTest = ccbTest ? ccbTest : L"null string";
return 0;
}
当编译器想要在pTest 中存储一个指针时,它会使用一个临时的CComBSTR。然后它使用CCcomBSTR 类中可用的BSTR 转换和临时值,并将指针存储在pTest 中。然后临时对象被销毁,我在pTest 中留下了一个悬空指针。
解决方法是投射CComBSTR:
const wchar_t * pTest = ccbTest ? static_cast<BSTR>( ccbTest ) : L"null string";
我不明白为什么需要修复。我认为编译器只会尝试自行转换为BSTR。为什么是临时的?
【问题讨论】:
标签: c++ visual-studio-2010 atl ternary-operator bstr