473,473 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to use using in header file?

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?

--
Elias
Jul 22 '05 #1
2 1712
lallous wrote:
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?

--
Elias


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

Jul 22 '05 #2

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:40**************@sbcglobal.net...
lallous wrote:
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?

--
Elias
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<std::string> StringVecImp;
// ...
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?

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leslie Houk | last post by:
I'm just learning PHP, and I'm having a problem using fpassthru(). I want to download a file using the following code segment I found, which I put into my testing file called portal.php: ...
4
by: Newsgroup - Ann | last post by:
I have a library function like GetOptions( ..., struct net_arch_t netArch, ....) and put the declaration into a regular header file like getopt.h. But this function declaration also needs the...
2
by: trying_to_learn | last post by:
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...
0
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: nail | last post by:
Hi. I have this xml file: <menu> <header caption="File"> <item header="File" caption="New File"></item> <item header="File" caption="Open File"></item> <item header="File" caption="Close...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
0
by: jinnareddy | last post by:
Hi, I'm unable to download a file that is having a 2-byte char in its name (e.g.テ) using force download option. Though, am able to download file names involving ASCII chars. I have tried URL...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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 project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.