473,779 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6803
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
1952
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
6402
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 please confirm or deny? TIA, - J.
17
3517
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 using std::cin; using std::cout;
4
2711
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
4527
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 : c:\Research\Code\Visual Studio\TestC++2\TestC++2.h(7): error C2871: 'std' : a namespace with this name does not exist If I try this with other project types, e.g. "Console Application (.NET)" projects, I have no such problem.
30
4122
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
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.