C++ 在C++;?
关于这一主题,我有一些问题,但首先让我给你一些代码: 示例.hppC++ 在C++;?,c++,C++,关于这一主题,我有一些问题,但首先让我给你一些代码: 示例.hpp #ifndef EXAMPLE_H_ #define EXAMPLE_H_ using namespace std; void say_something(string something); #endif #include <string> #include <iostream> #include "example.hpp" void say_something(string somethin
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
using namespace std;
void say_something(string something);
#endif
#include <string>
#include <iostream>
#include "example.hpp"
void say_something(string something)
{
cout << something << '\n';
}
#include <iostream>
#include <string>
#include "example.hpp"
using namespace std;
int main()
{
string hello = "Hello world!";
say_something(hello);
return 0;
}
示例.cpp
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
using namespace std;
void say_something(string something);
#endif
#include <string>
#include <iostream>
#include "example.hpp"
void say_something(string something)
{
cout << something << '\n';
}
#include <iostream>
#include <string>
#include "example.hpp"
using namespace std;
int main()
{
string hello = "Hello world!";
say_something(hello);
return 0;
}
#包括
#包括
#包括“example.hpp”
虚空说某事(串某事)
{
库特
- 我应该将example.cpp需要的所有头文件都放在example.hpp头文件中,还是像上面的示例一样将它们保存在example.cpp头文件中
将头文件包含在每个需要它们的文件中。不要担心重复
- C++如何在这种情况下工作:在“强>主.CPP<强”和“强>例”中,“string”和“iOffString”都是C++编译器要链接的(或者任何你能找到的更符合条件的术语)使用字符串和iostream头将程序运行两次?我是否应该仅将字符串和iostream头放入示例.cpp
头文件本身确保它的内容在任何翻译单元中只包含一次。这是通过#ifdef EXAMPLE_H
测试实现的
- 最后,我应该将示例头使用的名称空间放在头本身(example.hpp)还是放在实现(example.cpp)中
从不将使用名称空间…
放入头文件的全局范围ever
如果您必须使用使用名称空间…
只能在您自己的名称空间或函数中使用它。即使如此,也不建议将它用于大型名称空间,如std
,其中包含数千个大家一直使用的非常常见的符号
此外,理想情况下,您应该只在源.cpp
文件中使用使用名称空间…
。但是(如前所述),对于逻辑上应该存在于您自己的名称空间中的小型名称空间,有时将其包含在标头的非全局部分是合适的
我应该把example.cpp需要的所有头文件都放在example.hpp头文件中,还是像上面的例子那样放在example.cpp头文件中
保持头文件最小
头文件提供函数声明。函数声明取决于
中的内容,而不是
中的内容。因此头文件应包括
而不包括
如果头文件包含不必要的内容,则头文件的用户也将包含这些不必要的内容
< C++ >如何在这种情况下工作:在Me.CPP和Copy.CPP中都包含了“string”和“IoString”……C++编译器会用字符串和IoSoad头来链接程序(或者你能找到更合适的术语),我应该把这个字符串和IOSROW头放到这个例子中吗?CPP
C++源代码不与标题链接。每当您编写#include
时,预处理器(概念上)复制名为“string”的整个标题文件,并粘贴在#include
行。预处理在编译和链接之前进行
最后,我应该将用于示例头的名称空间放在头本身(example.hpp)还是放在实现(example.cpp)中
切勿在头文件的全局范围内使用命名空间std;
写入。由于#include
的复制和粘贴性质,using指令将感染包含头文件的其他文件。如果有一个文件想要使用说些什么,但不想使用命名空间std
我应该把example.cpp需要的所有头文件都放进去吗
标题example.hpp,还是应该像这样保存在example.cpp中
上面的例子是什么
不,不应该。头文件应该只包含它需要的头文件
< C++ >如何在这种情况下工作:在Me.CPP和Copy.CPP中都包含了“string”和“IoString”……C++编译器会用字符串和IoSoad头来链接程序(或者你能找到更合适的术语),我应该把这个字符串和IOSROW头放到这个例子中吗?CPP
由于包含保护,编译器不会两次链接string
或iostream
。例如,它只会打开字符串头
,并在包含保护告诉编译器它已经被包含后立即返回
最后,我应该将用于示例头的名称空间放在头本身(example.hpp)还是放在实现(example.cpp)中
这与包含问题相同。如果在头文件中放置“使用名称空间std;
”,则包含它的所有其他文件都将被迫使用整个名称空间。顺便说一句,这不是一件好事
因此,在实现中使用namespace std
并非坏事(在包含所有头之后)
在头文件中,也可以在函数体内部使用“使用命名空间std
”或“使用std::string
”,它们将仅限于函数体的范围
void somef(std::string str_arg)
{
using std::string;
string str;
// This is not evil either.
using namespace std;
string str;
}
void somef2() {
//string str; //error
}
如果somef
是类的一种方法,则常用的方法是使用typedef,例如:
class MyClass
{
typedef std::string string_type;
//using string_type = std::string; //C++11
string_type data_member;
void somef(string_type str)
{
string_type local_str;
}
void somef2() {
string_type local_str; // works
}
};
#包括
#包括
#包括“example.hpp”
我想说,使用头文件的顺序也是错误的。假设示例。hpp
使用std::string
。这不会引发编译错误,因为您在示例.hpp
之前包含了
如果您在另一个文件中使用example.hpp
,而之前不包含
,会发生什么情况?您将得到一个编译错误,因为您的头