Program #1
#include <iostream>
#include <alphabets.h>
letter alps {'a','b',....}; //alps goes in global namespace
So your variable is now accessible globally without creating any object or using functions. It is global for all those who use the header file of above code.
One more e.g. #include <iostream> is a header file provided by compiler. So it contains all functions in namespace std. Since std is global, including iostream allows us to access all methods withut :: operator.
If you use gcc v3 and above you will find that std is no more global. You have to write std::cin for cin.
Thanks,
So I have come across topics recommending not using the global namespace and global namespace pollution, is this because of the possibility of naming conflicts or is there some other reason? Surely the compiler would pick these conflicts up!
Can you create objects on the heap from the global namespace, and it so where are they/should they be deleted?
The C++ language provides a single global namespace. This can cause
problems with global name clashes. For instance, consider these two C++
header files:
char func(char);
class String { ... };
// somelib.h
class String { ... };
With these definitions, it is impossible to use both header files in a single
program; the String classes will clash.
A namespace is a declarative region that attaches an additional identifier to
any names declared inside it. The additional identifier makes it less likely
that a name will conflict with names declared elsewhere in the program. It
is possible to use the same name in separate namespaces without conflict
even if the names appear in the same translation unit. As long as they
appear in separate namespaces, each name will be unique because of the
addition of the namespace identifier. For example:
Code:
namespace one
{
char func(char);
class String { ... };
}
// somelib.h
namespace SomeLib
{
class String { ... };
}
Now the class names will not clash because they become one::String and
SomeLib::String, respectively.
Declarations in the file scope of a translation unit, outside all namespaces,
are still members of the global namespace
Thanks,
So I have come across topics recommending not using the global namespace and global namespace pollution, is this because of the possibility of naming conflicts or is there some other reason? Surely the compiler would pick these conflicts up!
Can you create objects on the heap from the global namespace, and it so where are they/should they be deleted?
The global namespace is "the area" surrounding any namespaces declared explicitely, regardless whether these are named or not. The purpose of namespaces is to group together things that logically belong together. Because of this, if you bloat a namespace into the global namesace (a.k.a "pollute the global namespace") you distroy this logical grouping. As a side effect, the compiler won't be able any more to distinguish between constructs that have the same name but belonged to different namespaces. In most cases the compiler will pick the conflict up and generate an error but there are cases where the compiler will be able to solve the conflict on its own in a way unintended by the programmer. This leads to hard to track bugs. Example:
Code:
namespace my_namespace
{
void f(int i)
{
// do something
}
}
void f(float x)
{
// do something else);
}
//version 1:
using namespace my_namespace;
// version 2: comment out the above line
int main()
{
f(1); // version 1: will call my_namespace::f(1), because it's the better match
// version 2: will call the f(1) in the global namespace
return 0;
}
Creating objects (on the free store -- a.k.a. heap -- or otherwise) has nothing to do with namespaces. If you say
Code:
A* a = new A();
the compiler will search for the type "A" beginning with the innermost namespace (the one where your line of code lives in) up to the global namespace and pick the first A that qualifies. The heap itself doesn't know about namespaces at all.
Gabriel, CodeGuru moderator
Forever trusting who we are
And nothing else matters - Metallica
One more e.g. #include <iostream> is a header file provided by compiler. So it contains all functions in namespace std. Since std is global, including iostream allows us to access all methods withut :: operator.
If you use gcc v3 and above you will find that std is no more global. You have to write std::cin for cin.
You can make std appear global by putting
Code:
using namespace std;
into your code. Then you will no longer need to use std:: but this is bad practice to do on a global level. It is reasonable on a local level.
Local level would mean:
- in a compilation unit (.cpp file) rather than a header
- in a header but within another namespace. Then std is only included within your namespace
- in a header file but within a class definition. Then std is only included within that class.
You may like to visit this tutorial - Namespaces. I have attached an image that might help you understand the global namespace better. You could consider the global namespace as a superset of all the namespace and all code would be inside this global namespace (code inside individual namespace are contained in those namespaces respectively but that is again there inside the global one).
Last edited by exterminator; August 9th, 2005 at 07:51 AM.
- in a header file but within a class definition. Then std is only included within that class.
Last point is incorrect as this is actually disallowed.
Code:
#include <vector>
namespace NS {}
struct A { void foo(); };
class B : A{
using A::foo; // ok
using std::vector; // error, using declaration disallowed, non-member
using namespace std; // error, using directive disallowed at class scope
using namespace NS; // error, using directive disallowed at class scope
};
Last point is incorrect as this is actually disallowed.
Code:
#include <vector>
namespace NS {}
struct A { void foo(); };
class B : A{
using A::foo; // ok
using std::vector; // error, using declaration disallowed, non-member
using namespace std; // error, using directive disallowed at class scope
using namespace NS; // error, using directive disallowed at class scope
};
I've come across this problem of not being able to use a using declaration inside a class and I came up with a very neat solution:
Code:
namespace mediator {
using std::vector;
class myclass
{
...
}; // class myclass
} // namespace mediator
using mediator::myclass;
This has the same effect as if you would put the usind std::vector in the class definition.
Old Unix programmers never die, they just mv to /dev/null
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.