如何理解C++ 临时变量的常量性

上传:icebug91033 浏览: 17 推荐: 0 文件:PDF 大小:54.23KB 上传时间:2020-12-22 13:13:27 版权申诉
1.认识临时变量的常量性 关于临时变量的常量性,先看一段代码。 void print(string& str) { cout<<str<<endl; } //如此调用会报编译错误 print("hello world"); 在Linux环境使用g++编译,会出现: invalid initialization of non-const reference of type ‘std::string&’ from a temporary of type ‘std::string’的错误。其中文意思为临时变量无法为非const引用初始化。出错的原因是编译器根据字符串”he
上传资源
用户评论