如何理解C语言中临时变量的常量性及其不可变性的本质?
- 内容介绍
- 文章标签
- 相关推荐
本文共计953个文字,预计阅读时间需要4分钟。
1. 认识变量临时变化的规律关于变量临时变化的规律,先看以下代码段:cppvoid print(string str){ cout <
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引用初始化。
本文共计953个文字,预计阅读时间需要4分钟。
1. 认识变量临时变化的规律关于变量临时变化的规律,先看以下代码段:cppvoid print(string str){ cout <
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引用初始化。

