how to use using in header file?

L

lallous

Hello

file.h
#include <string>
#include <vector>

//spot1
class StringVec
{
private:
std::vector<std::string> list;
public:
void add(const std::string val);
};

etc...

Now, since "file1.h" will be used in other source code modules, it is not
good to put "using namespace std" @ spot1.
How can I then declare that StringVec class is using 'std' namespace so that
I don't write 'std::XXX' everytime in the class?
 
T

Thomas Matthews

lallous said:
Hello

file.h
#include <string>
#include <vector>

//spot1
class StringVec
{
private:
std::vector<std::string> list;
public:
void add(const std::string val);
};

etc...

Now, since "file1.h" will be used in other source code modules, it is not
good to put "using namespace std" @ spot1.
How can I then declare that StringVec class is using 'std' namespace so that
I don't write 'std::XXX' everytime in the class?

file.h
#include <string>
#include <vector>
using std::string;
using std::vector

//spot1
class StringVec
{
private:
vector<string> list;
public:
void add(const string val);
};

Some notes:
1. Use a typedef for your container. This will make
life easier:
typedef vector<string> String_List;
// ...
String_List list;

2. Pass strings and other structures by reference not
by value:
void add(const string & val);
Notice the '&' after 'string'.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
N

Nick Hounsome

Thomas Matthews said:
file.h
#include <string>
#include <vector>
using std::string;
using std::vector

This is bad because you have bought std::string and std::vector into the
global namespace
for every file that includes this header.
//spot1
class StringVec
{
private:
vector<string> list;
public:
void add(const string val);
};

Some notes:
1. Use a typedef for your container. This will make
life easier:
typedef vector<string> String_List;

Better:

typedef std::string string;
typedef std::vector said:
// ...
String_List list;

Why would he want a class called StringVec to be implemented with a variable
called list which is actually a vector?
2. Pass strings and other structures by reference not
by value:
void add(const string & val);
Notice the '&' after 'string'.

Why not stick to STL terminology and call it push_back?
Why not just use std::vector?
 

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
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top