在c++中声明多个变量,是为所有这些变量声明的属性

Declaring multiple variables in C++ , is attributes declared for all those variables?

本文关键字:声明 变量 所有这些 属性 c++      更新时间:2023-10-16

如果我定义如下三个对象:

const string & textA = messages.at(0), 
               textB = messages.at(1), 
               textC = messages.at(2);

textBtextC实际上是一个参考吗?
我必须把&放在textBtextC前面吗?

谢谢!

textBtextC不是引用。把&看作是属于变量的,而不是属于类型的。

(刚用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