问题描述
我对头文件使用有以下疑问。
I have the following doubts on header files usage.
1 - 包括置于注释之后的警卫
c>使用在头文件中使用语句 - 不可能在包含它的源文件中取消使用某些东西,不应该强迫包含器向全局范围添加额外的东西。如果你需要使用头部中其他命名空间的东西,完全限定每个名字。Namespaces absolutely should be used in header files for declaring functions, classes, globals, etc. What you should not do is use using statements in header files -- it's impossible to unuse something in a source file that includes it, and you shouldn't force includers to add extra stuff to the global scope. If you need to use things from other namespaces in your headers, fully qualify every name. It can be a pain sometimes, but it's really the right thing to do.
示例:
// WRONG! using namespace std; class MyClass { string stringVar; }; // RIGHT class MyClass { std::string stringVar; };对于其他命名空间中的类的前向声明,只要记住在你的标题中引用 AnotherFoo 作为 AnotherNameSpace :: AnotherFoo 事实上,前向声明是打破循环依赖的唯一方法。
As for forward declarations of classes in other namespaces, you've got it exactly right. Just remember to always qualify AnotherFoo as AnotherNameSpace::AnotherFoo when you reference it inside your header. Indeed, forward declarations are the only way to break cyclic dependencies.
这篇关于使用C ++头文件的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!