Abr*_*ile 6 c++ string constants char
我对基本的C++用法有疑问.下面的代码,使用gcc/LInux编译,正确打印出来.
字符串test超出范围所以它的c_str()值也应该是无效的不是吗?我错了还是误解了const char*意思?
#include <iostream>
int main(){
const char* a = "aaaa";
std::cout << a;
{ std::string test("bbbb");a=test.c_str();}
std::cout << a;
a = "cccc";
std::cout << a;
}
aaaabbbbcccc
// print out without any problem
Run Code Online (Sandbox Code Playgroud)
Mat*_*Mat 13
你没错,你的代码无效,因为它使用的是一个生命周期已经结束的对象.它"偶然"工作,你不能依赖它.
| 归档时间: |
|
| 查看次数: |
1198 次 |
| 最近记录: |