简体   繁体   中英

C++: using namespace and #include

In C++ what is the difference between the #include directive and using namespace ? Also do you store your namespaces as separate files and what is the file extension for such files?

In C++, #include is used to add files to your project while namespace is used to keep your objects in logical modules (namespace does not apply to C)

For example, you might have a vector class in file " vector.h " so you include it in your project.

vector is a part of a large library (the standard library) STD, so you can access it with

std::vector

However since programmers are lazy and don't want to write std:: all over the place (the standard library has many many very useful parts), you can write

using namespace std

on the top of your file. This will tell the compiler that everytime it sees a type (such as vector), also check in the namespace std because the definition might be there. This way, the following statements become equivalent.

std::vector
vector

In vector.h, you should see something like

namespace std
{
   class vector { /* Implementation */ }
}

So #include is to add files, while using namespace is to keep your code cleaner and packaged in "meaningful" libraries. You can omit using namespace when programming but you absolutely need the #include

In order to answer your question, I will go a little back and take some basics of C and C++.

When compiling C/C++, the compiling the source files into an actual executable is actually two steps, compiling and linking. The compilation step takes one .cpp file at a time and compiles this. The contents of other .cpp files are not visible to the compiler. This generates an "object file" (I don't know why it's called such). All the object files are then linked by the linker to produce the final executable.

This brings in two important concepts in C++, declarations and definitions. A declaration specifies that something (a variable or a function) will exists somewhere. The following is the declaration of function Foo()

void Foo();

This means that we have told the compiler that somewhere there will be a function Foo() that takes no arguments and return no value.

A definition specified what function actually does. Here the function is defined

void Foo() { cout << "Foo!!"; }

Lets define another function, Bar()

void Bar() {
    Foo();
    cout << "bar";
}

This function calls function Foo(). This function cannot be compiled if function foo has not already been either declared or defined previously in the same file. So the declaration does not in itself produce any compiled code. They just have to be there.

If the function Foo() is not defined in this file, but a different .cpp file, then it is the job of the linker to make the connection between these two functions. If the function Foo(), is not defined anywhere you will get a linker error, not a compiler error.

This comes to the concept of header files. Header files are the place where you store your declarations. When using #include to include the contents of the header file, then what actually happens is that the preprocessor (a step that is executed before the actual compiler) will load the included file and "paste" the contents into the original source file. So the compiler will see the file as if the entire header file actually was pasted into the c++ file.

So when you program in C++, you will normally place your definitions in .cpp files, and you will place your declarations in .h files

Namespaces on the other hand is simply a way to logically group your code.

So no, namespaces are not stored in separate files, and they do not have a specific file extension. If I had a project with multiple namespaces I might create a separate directory for each namespace (and then again, I might not, it would depend on the situation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM