469,646 Members | 1,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

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 & ?

May 25 '07 #1
6 1316
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
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
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
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
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
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.

Similar topics

92 posts views Thread by Reed L. O'Brien | last post: by
39 posts views Thread by VidTheKid | last post: by
669 posts views Thread by Xah Lee | last post: by
9 posts views Thread by Cao Yi | last post: by
5 posts views Thread by thermate | last post: by
89 posts views Thread by Tubular Technician | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.