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

using namespace directive in header file

P: n/a
im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
qn)how do i resolve the requirement of *not* using the "using" directive
in the header file and at the same time declare my structure in the
header file?.Pls excuse me if this is a dumb qn. im just starting out

#ifndef FIRSTOOPSTRUCT_H
#define FIRSTOOPSTRUCT_H

struct firstoopstruct
{
int data;
void behav(string);
};
#endif
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"trying_to_learn" <no****@no.no> wrote in message
im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
qn)how do i resolve the requirement of *not* using the "using" directive
in the header file and at the same time declare my structure in the
header file?.Pls excuse me if this is a dumb qn. im just starting out

#ifndef FIRSTOOPSTRUCT_H
#define FIRSTOOPSTRUCT_H #include<string>
struct firstoopstruct
{
int data;
void behav(string);
void behav(std::string);
};
#endif


Address string as std::string.

Sharad
Jul 22 '05 #2

P: n/a
On Sun, 31 Oct 2004 23:34:17 -0800, trying_to_learn <no****@no.no>
wrote:
im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
You do not have to do it this way. Unfortunately, most example
programs do this, but it is a bad habit.
qn)how do i resolve the requirement of *not* using the "using" directive
in the header file and at the same time declare my structure in the
header file?.Pls excuse me if this is a dumb qn. im just starting out

#ifndef FIRSTOOPSTRUCT_H
#define FIRSTOOPSTRUCT_H

struct firstoopstruct
{
int data;
void behav(string);
};
#endif


You must either write std::string, instead of just string, or you can
use a typedef:

typedef std::string string;

I always write out std::string (or use a different typedef alias)
because it documents to other programmers, who may have to maintain
your code in the future, the fact that you did not have "using
namespace std;" in this or some other header file. IOW, if I were the
"other programmer", and saw "string" written throughout your code but
overlooked the typedef, I might have come to the erroneous conclusion
that there is a "using namespace std;" somewhere else and waste some
time looking for it.

Note that it is legal C++ to write "using namespace std;" in a header
file, but the results of doing so can be catastrophic ... please take
the trouble to understand WHY it is strongly recommended not to do so
(google for "namespace pollution", for example). It is perfectly OK to
put it in a .cpp file, but only AFTER including any other headers.
Better yet, use it only inside each function where it is needed, or
put "using std::string;"-type declarations inside the functions
(google for the difference between "using directive" and "using
declaration" ... I always mix them up, anyway <g>).

BTW you might consider declaring the function "behav" like this:

void behav(std::string const &);

This will usually perform better because it eliminates the extra
temporary string copy you would otherwise have.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.