473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 1498
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
39
by: VidTheKid | last post by:
THE PROBLEM The % symbol is too vague when defining dimensions in CSS and HTML. It can relate to an inherited value, a measure of the containing element (which can differ between box models) or...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
9
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <=...
5
by: thermate | last post by:
Dr.Bricmont, We are extremely proud of you for your brilliant analysis and courage to come out and say it. Your article brings out not one but several key concepts, worth close reading. The...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.