【问题标题】:how to test if CComBSTR is empty如何测试 CComBSTR 是否为空
【发布时间】:2011-09-17 01:20:36
【问题描述】:

如何测试 CComBSTR 是否为空字符串? (没有'text'值,可以是""也可以是null)

我的想法:

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

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

谢谢。

【问题讨论】:

    标签: string com visual-studio-2005 atl bstr


    【解决方案1】:

    4) 测试长度为 0 它存储起来很快

    【讨论】:

    • 实际上它导致SysStringLength()被调用,这不是世界上最快的事情。
    • 为什么这么慢?它们是长度前缀。
    【解决方案2】:

    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 */ }
    

    【讨论】:

      猜你喜欢
      • 2011-02-27
      • 1970-01-01
      • 2019-03-22
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多