在c++中声明多个变量,是为所有这些变量声明的属性
Declaring multiple variables in C++ , is attributes declared for all those variables?
如果我定义如下三个对象:
const string & textA = messages.at(0),
textB = messages.at(1),
textC = messages.at(2);
textB
和textC
实际上是一个参考吗?
我必须把&
放在textB
和textC
前面吗?
谢谢!
textB
和textC
不是引用。把&
看作是属于变量的,而不是属于类型的。
(刚用g++检查过)
使用这个符号,你会看到发生了什么:
const string &textA = .., // reference
&textB = .., // reference
textC = ..; // value
同样适用于指针:
const string *textA = .., // pointer
*textB = .., // pointer
textC = .. ;// value
结合const string *textA = .., // pointer
&textB = .., // reference
textC = .. ;// value
相关文章:
- 在疯狂的部分中声明变量
- 如何在C++中为高分辨率时钟声明变量?
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- CUDA 的性能取决于声明变量
- 如何在不为其声明变量的情况下获取和使用常量值的地址?
- C++声明变量时自动类型推断而不初始化
- 在不同循环中多次声明变量的优点
- 奇怪的错误 C2131 与 constexpr 声明变量
- 是否可以在 "if" 语句中声明变量?
- 在python-ctypes中声明变量并传递给dll函数
- 在递归函数C++中声明变量
- 只有一个定义/声明时标头声明变量的多堆定义错误
- 奇怪的未声明变量编译器错误
- 我在C++程序中声明变量时遇到问题
- 在命名空间中声明变量
- C++ lambda 按值捕获,而无需更早声明变量
- 声明变量以保存字符串列表时的内存分配
- 如何声明C 变量应突变
- 为什么允许在开关语句中声明变量?但不是声明 初始化
- 在同一命名空间中声明变量和函数是否出错?[C++]