daisy
daisy

Reputation: 23511

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

If i define three objects like the following:

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

Are textB and textC actually a reference?
Do I have to place the & in front of both textB and textC?

Thanks!

Upvotes: 2

Views: 415

Answers (2)

stefan
stefan

Reputation: 2886

use this notation instead and you will see what happens:

const string     &textA = .., // reference
                 &textB = .., // reference
                 textC = ..; // value

Same thing applies to pointers:

const string     *textA = .., // pointer
                 *textB = .., // pointer
                 textC = .. ;// value

Combined

const string     *textA = .., // pointer
                 &textB = .., // reference
                 textC = .. ;// value

Upvotes: 2

Deltik
Deltik

Reputation: 1137

textB and textC are not references. Think of the & as if it belongs with the variable, not the type.

(Just checked with g++)

Upvotes: 5

Related Questions