|
From: Ioannis V. <no...@ya...> - 2002-03-22 05:07:36
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Daniel
Glenfield
Sent: Thursday, March 21, 2002 8:13 PM
To: dev...@li...
Subject: [Dev-C++] Namespaces
Could someone explain this to me a little bit clearer? Is it just saying
you need to write the namespace declaration to write headers without the
.h extention? If so what's the point in that?
Thanks,
Daniel
=20
Things are slightly different:
=20
=20
C++ also supports namespaces. Namespaces simplify the writing of
programs that combine libraries from various members. The namespace
feature is not available in C. The statement that must be included in
the program when using namespaces is using namespace std;.
=20
or std::facility.=20
=20
=20
e.g.
// This is a program to demonstrate the use of namespace.
#include <iostream>
using namespace std;=20
=20
Bad use of namespace in global scope.=20
void main()=20
void main() is not valid C++ anymore.
=20
{
cout << "Using Namespace";
}
In C++, if you use iostream instead of iostream.h, the namespace
directive is used to make the definitions in iostream available to your
program.
=20
iostream.h is not valid C++ anymore. The .h exttension is valid for the
C subset header files only.
=20
=20
Now lets explain what is a namespace. Namespace are name-scopes and its
purpose is to help using various libraries. As all features of C++ this
is a great feature too.
=20
Consider the code:
=20
#include <iostream>
#include <string>
=20
#include <daniel.h> // Contains Daniel's implementation of cout in the
daniel namespace.
=20
int main()
{
using namespace std;
=20
string s;
=20
cin>>s;
=20
daniel::cout<<s<<daniel::endl;
}
=20
=20
We could also write
=20
using daniel::cout;
using daniel::endl;=20
=20
inside main().
=20
=20
We must try to keep the using namespace name_space; and using
name_space::facility_name; as in local scope as possible. Declaring
these in the global namespace is an effort to defeat the namespace
system (bad for you).
=20
=20
Ioannis
=20
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
|