confusion: include and namespace

P

puzzlecracker

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
 
K

Ken Human

puzzlecracker said:
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.
 
R

Rolf Magnus

puzzlecracker said:
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>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Include Files Directory Structure 7
Chatbot 0
TF-IDF 2
namespace and #include 4
namespace confusion ? 2
include namespace declarations 2
help with namespace/friend error 3
Namespace and #Include best practises 14

Members online

Forum statistics

Threads
474,293
Messages
2,571,500
Members
48,187
Latest member
ashish_758

Latest Threads

Top