By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

What this means?

P: n/a
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 & ?

May 25 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Pe*********@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
May 25 '07 #2

P: n/a
Pe*********@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.
May 25 '07 #3

P: n/a
On 25 May, 16:51, Devon Null <theronnights...@xgmailx.comwrote:
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

May 25 '07 #4

P: n/a
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 ) { ... }
May 25 '07 #5

P: n/a
So what is the difference between these two?

void print_msg( ostream& os, const string& msg ) { ... }

void print_msg( ostream &os, const string &msg ) { ... }

May 25 '07 #6

P: n/a
Pe*********@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)
May 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.