By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

How to disable "using namespace"

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Peng Yu wrote:
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
Jul 22 '05 #2

P: n/a
Peng Yu wrote:
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;
Jul 22 '05 #3

P: n/a
Peng Yu wrote:
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.

Jul 22 '05 #4

P: n/a


Peng Yu wrote:
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
Jul 22 '05 #5

P: n/a
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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.