473,799 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing namespaces

Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?
Jul 22 '05 #1
21 1896
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@ho tmail.com> wrote:
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?


You aren't changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many 'using namespace ...' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error 'ambiguous name cout' or some such.

john
Jul 22 '05 #2
Blue Ocean wrote:
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}


I think you misunderstand using directives. A using directive makes
names from a namespace available as if they had been declared in the
global namespace. The second using directive in your example doesn't
nullify the effect of the first one. Assuming there is a polygon::cout,
the code would fail to compile, because there would be a name conflict.

Since you're a Java programmer, this example might help:

import java.awt.*; // The java equivalent of a using directive.
import java.util.*;

public class Foo
{
public static void main (String[] args)
{
List list;
}
}

This Java code suffers from pretty much the same problem as your C++
example. Try to compile it and you'll see what I mean.

--
Russell Hanneken
eu*******@cbobk .pbz
Use ROT13 to decode my email address.
Jul 22 '05 #3
John Harrison wrote:
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@ho tmail.com> wrote:
Forgive me for asking so many questions so quickly. I am simply
asking as they come up unanswered in my book.

Can you change namespaces half way through a file? For instance, if
you want to use std and then half way through switch to your own:
{
...
using namespace std;
cout << "doing polygons";
using namespace polygon;
cout << poly1;
...
}

I know that this is a ridiculous example. Why would anyone define an
outstream for a 3D object with the name cout. But, suppose they did,
would this code snippet work, or do you have to use just one namespace
per file and be explicit about the use of other namespaces?


You aren't changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many 'using namespace ...' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error 'ambiguous name cout' or some such.

john


Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?

So if you have "std:ios:in " in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?

Jul 22 '05 #4
JustSomeGuy posted:
using namespace std;
using namespace system;
using namespace mine;


This will dump everything into the global namespace.

Not only can you just write:
string pp;
You can write:

::string pp;
Ahh!!
-JKop
Jul 22 '05 #5
JustSomeGuy wrote:

Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?
Sure.
So if you have "std:ios:in " in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?


I think you meant to type semicolons rather than colons at the end of
your using directives. Anyway, that wouldn't work, because ios is a
class, not a namespace.

--
Russell Hanneken
eu*******@cbobk .pbz
Use ROT13 to decode my email address.
Jul 22 '05 #6
On Wed, 07 Jul 2004 15:34:37 -0600, JustSomeGuy <No***@ucalgary .ca> wrote:
John Harrison wrote:
On 7 Jul 2004 13:34:39 -0700, Blue Ocean <bl*********@ho tmail.com>
wrote:
> Forgive me for asking so many questions so quickly. I am simply
> asking as they come up unanswered in my book.
>
> Can you change namespaces half way through a file? For instance, if
> you want to use std and then half way through switch to your own:
> {
> ...
> using namespace std;
> cout << "doing polygons";
> using namespace polygon;
> cout << poly1;
> ...
> }
>
> I know that this is a ridiculous example. Why would anyone define an
> outstream for a 3D object with the name cout. But, suppose they did,
> would this code snippet work, or do you have to use just one namespace
> per file and be explicit about the use of other namespaces?
You aren't changing namespaces, you are adding to the set of namespaces
that will be searched. You can have as many 'using namespace ...' as you
like and all of them will be searched.

In your example, if cout was defined in namespace std and polygon, then
you would get a compiler error 'ambiguous name cout' or some such.

john


Wow.. So if you put:

using namespace std;
using namespace system;
using namespace mine;

at the top of your code then all three namespaces will be searched... and
assuming there is no conflict they will be resolved?


Right

So if you have "std:ios:in " in your code, you could change it for
using namespace std:
using namespace ios:
and then just specify "in"?


No because ios is not a namespace, its a typedef for a template. But yes,
in general you're right.

namespace outer
{
namespace inner
{
int xxx;
}
}

using namespace outer;
using namespace inner;

int main()
{
xxx = 1;
}

john
Jul 22 '05 #7
On Wed, 07 Jul 2004 21:41:41 GMT, JKop <NU**@NULL.NULL > wrote:
JustSomeGuy posted:
using namespace std;
using namespace system;
using namespace mine;


This will dump everything into the global namespace.

Not only can you just write:
string pp;
You can write:

::string pp;
Ahh!!


Interesting, but ugly. I suppose that demonstrates that it's not really
the case that namespaces are searched, but rather that the names from a
namespace are put into the global namespace.

john

Jul 22 '05 #8
JKop <NU**@NULL.NULL > wrote in news:p6******** *********@news. indigo.ie:
JustSomeGuy posted:
using namespace std;
using namespace system;
using namespace mine;


This will dump everything into the global namespace.

Not only can you just write:
string pp;
You can write:

::string pp;


Somebody correct me if I'm wrong, but this is bad info.

I think of the using namespace declarations as adding to a search path for
identifiers, not actually copying them into other namespaces.

The "::string pp" is explicitly looking for a "string" identifier in the
global namespace, and no other namespaces. Thus the "::string" would not
be the same as "std::strin g".
Jul 22 '05 #9
Andre Kostur wrote:
JKop <NU**@NULL.NULL > wrote in news:p6******** *********@news. indigo.ie:
JustSomeGuy posted:
using namespace std;


This will dump everything into the global namespace.

Not only can you just write:

string pp;

You can write:

::string pp;


Somebody correct me if I'm wrong, but this is bad info.


I think you're right. ::string fails to compile on GCC 3.3.3 and the
Comeau Online Compiler.

--
Russell Hanneken
eu*******@cbobk .pbz
Use ROT13 to decode my email address.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
3053
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
11
1795
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I understand that you declare your intention to use a namespace within a page through the "Inherits" attribute. I know that using "Inherits" isn't absolutely necessary, it's just recommended so the developer doesn't have to type out the entire...
0
9688
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...
1
10238
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
10030
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
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4145
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
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.