|
|
 |
 |
 |
 |
What this means?
void print_msg( ostream &os, const string &msg ) { if ( msg.empty() ) // nothing to print; terminate function ... return; os << msg; }
I don't understand the first line : void print_msg( ostream &os, const string &msg ) by writing ostream &os,you do what ? what that means? same goes for const string &msg ... why not just const string msg,without the & ?
PencoOdS @gmail.com wrote: > void print_msg( ostream &os, const string &msg ) > { > if ( msg.empty() ) > // nothing to print; terminate function ... > return; > os << msg; > } > I don't understand the first line : void print_msg( ostream &os, const > string &msg ) > by writing ostream &os,you do what ? what that means? same goes for > const string &msg ... > why not just const string msg,without the & ?
What book on C++ are you reading that doesn't explain references? The declaration T &t; means that 't' is a reference to an object of type 'T'. Look them up. V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask
PencoOdS @gmail.com wrote: > void print_msg( ostream &os, const string &msg ) > { > if ( msg.empty() ) > // nothing to print; terminate function ... > return; > os << msg; > } > I don't understand the first line : void print_msg( ostream &os, const > string &msg ) > by writing ostream &os,you do what ? what that means? same goes for > const string &msg ... > why not just const string msg,without the & ?
It passes the address of the parameters the function is called with. It allows you to directly alter the data with out having to return a specified value. It is much easier to handle that way when doing complicated action, if you are careful. Also it is much more efficient - less overhead.
On 25 May, 16:51, Devon Null <theronnights@xgmailx.com> wrote:
> PencoOdS @gmail.com wrote: > > void print_msg( ostream &os, const string &msg ) > > { > > if ( msg.empty() ) > > // nothing to print; terminate function ... > > return; > > os << msg; > > } > > I don't understand the first line : void print_msg( ostream &os, const > > string &msg ) > > by writing ostream &os,you do what ? what that means? same goes for > > const string &msg ... > > why not just const string msg,without the & ? > It passes the address of the parameters the function is called with.
No it doesn't. That answer would be appropriate if the OP had asked about pointers, but they didn't - they asked about references. Gavin Deane
On May 25, 11:15 am, PencoOdS@gmail.com wrote:
> void print_msg( ostream &os, const string &msg ) > { > if ( msg.empty() ) > // nothing to print; terminate function ... > return; > os << msg; > } > I don't understand the first line : void print_msg( ostream &os, const > string &msg ) > by writing ostream &os,you do what ? what that means? same goes for > const string &msg ... > why not just const string msg,without the & ?
Because 'const string msg' would create a copy of the string being passed. Therefore invoking a costly copy constructor. Instead of constructing the string; pass it by const reference: 'const string& msg'. This creates an 'alias' of the original which can't be modified nor reseated. In C++, unless there is reason to do otherwise, passing by reference should be the default. This is specially critical if you consider 'ostream& os' in your function. You would definitely not enjoy the results of copy constructing a standard output stream (std::ostream) since you are probably passing your precious std::cout to that function to display messages on the console. I'ld suggest writing: void print_msg( ostream& os, const string& msg ) { ... } ... instead of the confusing ... void print_msg( ostream &os, const string &msg ) { ... }
So what is the difference between these two? void print_msg( ostream& os, const string& msg ) { ... } void print_msg( ostream &os, const string &msg ) { ... }
PencoOdS @gmail.com writes: > So what is the difference between these two? > void print_msg( ostream& os, const string& msg ) { ... } > void print_msg( ostream &os, const string &msg ) { ... }
Semantically, none whatsoever. The top version is usually, but not always, preferred by C++ programmers. The bottom version is usually, but not always, preferred by C programmers. What book are you reading that doesn't explain this? Get a better one! -- Dave Steffen, Ph.D. A Zen master once said to me, "Do the Software Engineer IV opposite of whatever I tell you." Numerica Corporation So I didn't. ph (970) 461-2000 x227 -- not Hofstadter (but should have been)
|
 |
 |
 |
 |
|