In c + + Primer in Chinese version 5 on page 237 of the constructor has the following code:
Struct Sales_data { //the new constructor Sales_data ()=default; Sales_data (const STD: : string & amp; S) : bookNo (s) {}//why use reference? Sales_data (const STD: : string & amp; S, unsigned n, double p) ://why 2 and 3 parameters need not refer to? What's the difference? Units_sold bookNo (s), (n), revenue (p * n) {} . }
Why want to do the first three constructor const STD: : string & amp; S to use references, I tried without references can also be, add references and without reference to what's the difference?
CodePudding user response:
& Is the purpose of reference, to avoid duplicate a STD: : string Const is to limit it read-only
Const string s words or copied again wouldn't it be a waste, since already a read-only why not directly use reference,