471,873 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

using namespace directive in header file

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
2 2751

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

Similar topics

17 posts views Thread by beliavsky | last post: by
6 posts views Thread by Gernot Frisch | last post: by
10 posts views Thread by Christian Christmann | last post: by
12 posts views Thread by Calum Grant | last post: by
13 posts views Thread by Squid Seven | last post: by
30 posts views Thread by Pep | last post: by
6 posts views Thread by Halcyon | last post: by
11 posts views Thread by subramanian100in | last post: by
reply views Thread by zermasroor | last post: by

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.