需要不可靠的CString plus-operator的解决方法

问题描述

我有以下旧代码

void CLayoutLine::UnicodeStr(CGlobalStr &Str,LPCTSTR Sep) const
{
    CString Line;   // buffer for words in line

    if (GetIndent())
    {
        // will be at least one tab
        int tabs = ((GetIndent()-1) / GetVM()->ParaIndent()) + 1;
    
        Line = CString(_T("\t"),tabs);
    }

    // add unsigned cast to eliminate warning - TBH 2017
    for (int bb = 0; (unsigned) bb < m_Boxes.size(); bb++)
    {
        CString Word = m_Boxes[bb]->Unicodestr();
        if (bb)
            Line = Line + Sep;
        Line = Line + Word;
    }

    Line = Line + Sep;       // add separator in case of more text - TBH 3/2018
    Str = Str + Line;        // += is not reliable with strings - TBH 10/2020
}

有时,此代码工作可靠;有时不是。当我调试它时,我看到了以下内容

Visual Studio Debugger Screenshot

在橙色框中执行代码行后,您可以看到Word的内容(绿色圆圈)为“消息”,但未附加到Line(红色圆圈)中! >

任何人都可以解释这种行为吗?还是这是Visual C ++ 2017中的错误?什么是最可靠的解决方法-直接使用Append()?还是我在CString + operator or Format中看到的Format()?我已经发现自己,CString的+ =运算符不可靠,如Different behaviour between CString operators “+=” and “+”

中所述

更新

我尝试更改为Append()(Visual Studio建议它是CString的成员函数,但我没有记录在案),但是我得到了相同的行为。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)