How to disable "using namespace"

R

Rolf Magnus

Peng said:
For example, at the beginning of the C++ program,
I say "using namespace std;"

However, in the middle of the program, I want to disable "using
namespace std;".

Do you know how to do that?

The answer is: not.
You can't do it. That's the reason why you shouldn't write "using namespace
std;" in the first place.
 
P

Peng Yu

For example, at the beginning of the C++ program,
I say "using namespace std;"

However, in the middle of the program, I want to disable "using
namespace std;".

Do you know how to do that?

Thanks!
Peng
 
V

Victor Bazarov

Peng said:
For example, at the beginning of the C++ program,
I say "using namespace std;"

However, in the middle of the program, I want to disable "using
namespace std;".

Do you know how to do that?

There is no way. Don't say 'using namespace std;' in the first place.

V
 
D

David Lindauer

Peng said:
For example, at the beginning of the C++ program,
I say "using namespace std;"

However, in the middle of the program, I want to disable "using
namespace std;".

Do you know how to do that?

Thanks!
Peng

As others have pointed out there is no way of undoing a using
statement... if you *must* do this modularize your program so that the
modules you do the using statement in actually need it and those that
don't need it don't get it. Another way is to reorganize a module and
put the dependencies on the standard name space at the end, so that the
first part of the file needn't have the using.

I think you've had enough people yell at you for doing it to begin with
tho :).

David
 
L

Lieven

Peng said:
For example, at the beginning of the C++ program,
I say "using namespace std;"

However, in the middle of the program, I want to disable "using
namespace std;".

Do you know how to do that?

Thanks!
Peng

I find it easier en more convenient to place std:: in front of what's in
this namespace.

cfr. std::cout << "easier" << std::endl;
 
J

JKop

Peng Yu posted:
For example, at the beginning of the C++ program,
I say "using namespace std;"

However, in the middle of the program, I want to disable "using
namespace std;".

Do you know how to do that?

Thanks!
Peng


You can limit it to only where you want it, as in:


#include <iostream>

int main()
{
using namespace std;

cout << "Monkey!";

void SomeFunc();

SomeFunc();
}


void SomeFunc()
{
cout << "Monkey!";

//Compile error
}



-JKop
 

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,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top