What this means?

P

PencoOdStip

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

Victor Bazarov

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
 
D

Devon Null

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.
 
G

Gavin Deane

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
 
S

Salt_Peter

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::eek:stream) 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 ) { ... }
 
P

PencoOdStip

So what is the difference between these two?

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

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

Dave Steffen

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,201
Latest member
JefferyBur

Latest Threads

Top