如何测试 CComBSTR 是否为空字符串?(没有“文本”值,可以是“”或可以为空)
我的想法:
- 测试是否
CComBSTR::ByteLength()
返回 0 - 测试是否
CComBSTR::GetStreamSize()
返回 0 - 测试是否
CComBSTR::m_str
为 NULL - 测试是否
CComBSTR::Length()
返回 0
哪一个是正确的方法?如果它们都不是,那么正确的方法是什么?
谢谢。
如何测试 CComBSTR 是否为空字符串?(没有“文本”值,可以是“”或可以为空)
我的想法:
CComBSTR::ByteLength()
返回 0CComBSTR::GetStreamSize()
返回 0CComBSTR::m_str
为 NULLCComBSTR::Length()
返回 0哪一个是正确的方法?如果它们都不是,那么正确的方法是什么?
谢谢。
4) 测试长度 0 它的存储速度很快
3) 测试 CComBSTR::m_str 是否为 NULL
如果您检查 CComBSTR 的源代码,您可以使用几个运算符来执行此测试:
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 */ }