2011-06-17 58 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 */ }