2011-06-17 102 views
2

如何测试CComBSTR是否为空字符串? (没有 '文' 的值,可以是 “” 或可以为null)如何测试CComBSTR是否为空

我的想法:

  1. 测试,如果CComBSTR::ByteLength()返回0
  2. 测试,如果CComBSTR::GetStreamSize()返回0
  3. 测试,如果CComBSTR::m_str是NULL
  4. 测试如果CComBSTR::Length()返回0

哪一个是正确进场?如果它们都不是,那么什么是正确的方法?

谢谢。

回答

2

4)测试长度为0它的速度快,因为它的存储

+0

事实上它导致'SysStringLength()'是卡勒这不是世界上最快的事情。 – sharptooth 2011-06-17 11:22:59

+2

为什么它很慢?它们的长度已经预先确定。 – Tom 2011-07-14 03:11:13

0

3)测试,如果的CComBSTR ::如果您选中的CComBSTR的源代码,有几家运营商可以用做这个测试m_str是NULL

bool CComBSTR::operator!() const throw() 
bool CComBSTR::operator!=(int nNull) const throw() 
bool CComBSTR::operator==(int nNull) const throw() 
operator CComBSTR::BSTR() const throw() 

例如:

CComBSTR value; 
if (!value) { /* NULL */ } else { /* not NULL */ } 
if (value != NULL) { /* not NULL */ } else { /* NULL */ } 
if (value == NULL) { /* NULL */ } else { /* not NULL */ } 
if ((BSTR) value) { /* not NULL */ } else { /* NULL */ }