By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

confusion: include and namespace

P: n/a
after reading some of the post I found out something rather radical to
my previous understanding: that when you do

#include<iostream>
#include<string>
#include<vector>
etc

compiler puts those .h files into the namespace std -.h files that
contain all the declarations for vector, string, etc...

and after using namespace std you import everything inside.
My previous understanding was that each of heretofore mentioned .h
files declaration were already contained within std namespace;
something like that:

iostream.h

namespace std
{
class sstream;
etc

....

}

string.h

namespace std
{
class basic_string;
etc

....

}
and so on..

By including those files, you just let a compiler know where those
definitions are located, and "using namespace" just brings those
declarations into the global namespace.
My question: what is going on, how does namespace and include
communicate? How can I tell compiler to include my .h files into my (or
maybe std) namespace?

reflection: I noticed that by NOT including #include<string>

Prefixing it with std (std::string.. std::endl) works... why, if it is
not put there by compiler (or is it a default)?


THANKS THANKS THANKS

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
puzzlecracker wrote:
after reading some of the post I found out something rather radical to
my previous understanding: that when you do

#include<iostream>
#include<string>
#include<vector>
etc

compiler puts those .h files into the namespace std -.h files that
contain all the declarations for vector, string, etc...

and after using namespace std you import everything inside.
My previous understanding was that each of heretofore mentioned .h
files declaration were already contained within std namespace;
something like that:

iostream.h

namespace std
{
class sstream;
etc

...

}

string.h

namespace std
{
class basic_string;
etc

...

}

The classes you mentioned are all conatined in the std namespace.
Typing "using namespace std;" does not "import" anything, rather it
tells the compiler to look in the namespace "std" for classes and
objects that it doesn't immediately recognize. Alternatively, if you
choose not to use a using statement, you must use fully qualified names
for objects and classes contained within a namespace. This means typing
std::string rather than string when you want to make a string object.
As another alternative, you can use a statement such as "using
std::string;" or "using std::cout;" as opposed to "using namespace std;"

and so on..

By including those files, you just let a compiler know where those
definitions are located, and "using namespace" just brings those
declarations into the global namespace.
My question: what is going on, how does namespace and include
communicate?
A "using" statement or qualified name tells the compiler where to look
when it sees something it doesn't recognize.

How can I tell compiler to include my .h files into my (or maybe std) namespace?
If you want to create your own namespace, look at the following code:

ken@ken-wn0vf73qmks ~/c
$ cat namespace.cpp
#include <iostream>
using namespace std;

namespace myNamespace {
void printCString(char *str) {
cout << str << endl;
}
}
int main() {
myNamespace::printCString("Hello World!");
return 0;
}

ken@ken-wn0vf73qmks ~/c
$ g++ -o namespace namespace.cpp

ken@ken-wn0vf73qmks ~/c
$ ./namespace
Hello World!

To the best of my knowledge, the std namespace is reserved, so you
shouldn't try to create anything new inside this namespace.

reflection: I noticed that by NOT including #include<string>

Prefixing it with std (std::string.. std::endl) works... why, if it is
not put there by compiler (or is it a default)?


If you don't include the string header, std::string won't work,
std::endl requires that you include iostream header. Prefixing anything
in a namespace in the way you describe is called using a fully qualified
name. It's how you tell the compiler where to look for something.


THANKS THANKS THANKS


Jul 23 '05 #2

P: n/a
puzzlecracker wrote:
after reading some of the post I found out something rather radical to
my previous understanding: that when you do

#include<iostream>
#include<string>
#include<vector>
etc

compiler puts those .h files into the namespace std -.h files that
contain all the declarations for vector, string, etc...
A header cannot be "in a namespace".
and after using namespace std you import everything inside.
My previous understanding was that each of heretofore mentioned .h
files declaration were already contained within std namespace;
something like that:

iostream.h

namespace std
{
class sstream;
etc

...

}

string.h

namespace std
{
class basic_string;
etc

...

}
and so on..

By including those files, you just let a compiler know where those
definitions are located, and "using namespace" just brings those
declarations into the global namespace.
Yes, and that's right. What makes you now believe that it's not?
My question: what is going on, how does namespace and include
communicate?
They don't communicate at all. #include is just kind of a copy/paste
functionality built into your C++'s preprocessor. It does nothing more than
just insert the code of the header in the place where you put the #include
directive. So actually, #include doesn't have anything to do with
namespaces at all.
How can I tell compiler to include my .h files into my (or
maybe std) namespace?
Standard C++ doesn't allow you to put your own classes into namespace std,
but putting them into your own namespace is easy. Just do it something
like:

namespace MyNamespace
{
class Foo
{
//definition of class Foo
void myfunction();
};
}

And for the implementation:

namespace MyNamespace
{
void Foo::myFunction()
{
//some code
}
}

or:

void MyNamespace::Foo::myFunction()
{
//some code
}
reflection: I noticed that by NOT including #include<string>

Prefixing it with std (std::string.. std::endl) works... why, if it is
not put there by compiler (or is it a default)?


Maybe you #included another header that itself #included <string>.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.