namespaces,

V

vsgdp

Hi,

Can you put a "using namespace" inside a class like this:

// c.h

#include <string>

class C
{
public:
using namespace std;

string myString;
};

I don't want to put it outside the class because then any .cpp file
that includes this header file will also get the using namespace std.
I just want to use using namespace std in my code, but not force
client code to use it.
 
V

Victor Bazarov

vsgdp said:
Can you put a "using namespace" inside a class like this:

// c.h

#include <string>

class C
{
public:
using namespace std;

string myString;
};

No. Expressly prohibited.
I don't want to put it outside the class because then any .cpp file
that includes this header file will also get the using namespace std.
I just want to use using namespace std in my code, but not force
client code to use it.

You'd be better off actually spelling 'std::' in the class definition,
but you're free to have a using directive in the source file where the
implementation of your class members live.

V
 
I

Ian Collins

vsgdp said:
Hi,

Can you put a "using namespace" inside a class like this:

// c.h

#include <string>

class C
{
public:
using namespace std;

string myString;
};

I don't want to put it outside the class because then any .cpp file
that includes this header file will also get the using namespace std.
I just want to use using namespace std in my code, but not force
client code to use it.
As Victor said, this is expressly prohibited. If you really want to,
you can write something like:

#include <string>

class C
{
public:
typedef std::string String;

String myString;
};
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top