小编Sou*_*tta的帖子

为什么编译器不抱怨将char添加到char*?

为什么c ++编译器不会抱怨以下代码?

#include<iostream>
int main()
{
    const char* p ="Hello";
    std::string q = p + 'H';
    std::cout << q << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

它正确地抛出了以下代码的错误

#include<iostream>
int main()
{
    const char* p ="Hello";
    std::string q = p + "World";
    std::cout << q << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译器抛出的错误语句

test.cxx: In function 'int main()':
test.cxx:5: error: invalid operands of types 'const char*' and 'const char [6]' to binary 'operator+'
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我理解,为什么第一个代码没有抛出任何错误声明?

c++ char-pointer

2
推荐指数
1
解决办法
82
查看次数

标签 统计

c++ ×1

char-pointer ×1