C ++:使用命名空间和#include [英] C++: using namespace and #include

查看:156
本文介绍了C ++:使用命名空间和#include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中, #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?

推荐答案

在编译C / C ++时,将源文件编译为实际的可执行文件实际上是两个步骤,编译和链接。编译步骤一次只需一个.cpp文件,并编译此文件。其他.cpp文件的内容对编译器不可见。这生成一个目标文件(我不知道为什么这样调用)。然后通过链接器链接所有的目标文件以产生最终的可执行文件。

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.

这在C ++,声明和定义中引入了两个重要的概念。声明指定某处(变量或函数)将存在于某处。下面是函数Foo()的声明:

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();

这意味着我们告诉编译器某处会有一个函数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!!"; }

允许定义另一个函数Bar()

Lets define another function, Bar()

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

此函数调用函数Foo如果函数foo尚未在同一文件中声明或定义,则此函数无法编译。所以声明本身不会产生任何编译代码。

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.

如果函数Foo()没有在这个文件中定义,而是一个不同的.cpp文件,那么它是链接器的工作以使这两个功能之间的连接。如果函数Foo()没有在任何地方定义,你将得到一个链接器错误,而不是编译器错误。

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.

这是头文件的概念。头文件是您存储声明的位置。当使用#include包含头文件的内容时,实际发生的是预处理器(在实际编译器之前执行的步骤)将加载包含的文件并将内容粘贴到原始源文件中。因此,编译器会看到文件,因为整个头文件实际上是粘贴到c ++文件中。

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.

所以当你在C ++中编程时, .cpp文件,你将你的声明放在.h文件中。

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).

这篇关于C ++:使用命名空间和#include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆