为什么当我在字符串中添加数字时,它会在c ++中显示随机文本?

Ãly*_*âñý -6 c++ string int

当我尝试将文本添加到字符串时,我得到随机值.

码:

#include <iostream>

using namespace std;

int main()
{
    cout << "333" + 4;
}
Run Code Online (Sandbox Code Playgroud)

我得到一些随机文字,如:??@

Вит*_*аев 5

"333"const char [4]不是std::string如你所料(其中的方式仍然没有operator+int).添加4,你转换const char *然后按4 * sizeof(char)字节移动指针,使其指向内存中带有垃圾的内存.