473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to disable "using namespace"

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
5 6751
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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
2
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
4
by: Teddy | last post by:
If I use just a STL container such as std::vector in my program. Is "using std::vector;" better than "using namespace std" ? Does "using namespace std" cost ?
6
by: AlexD_UK | last post by:
When I create a new C++ project of type "Class Library (.NET)", I am unable to then add the following line of code : using namespace std If I do, I get the following error on compilation :...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.