using namespace directive in header file

  • Thread starter trying_to_learn
  • Start date
T

trying_to_learn

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
 
S

Sharad Kala

trying_to_learn said:
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 said:
struct firstoopstruct
{
int data;
void behav(string);

void behav(std::string);
};


#endif

Address string as std::string.

Sharad
 
B

Bob Hairgrove

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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top