简体   繁体   English

为什么在 C++ .h 文件中设置宏定义和命名空间?

[英]Why there set macro definition and namespace in C++ .h file?

I am reading this open source project :我正在阅读这个开源项目

I have two questions:我有两个问题:

  1. I found the我找到了
#ifndef _RANGE_REQUEST_GENERATOR_H_
#define _RANGE_REQUEST_GENERATOR_H_
#endif

and I only find the _RANGE_REQUEST_GENERATOR_H_ in this .h file, other place not use it.我只在这个 .h 文件中找到了_RANGE_REQUEST_GENERATOR_H_ ,其他地方不使用它。

so, what's the usage of the macro definition?那么,宏定义的用途是什么?

  1. there use the namespace wrap the functions.那里使用命名空间包装函数。
namespace slowhttptest {
    //functions
}

what's the purpose?目的是什么? why do not use std , is it better?为什么不使用std ,是不是更好?

Question #1问题#1

See Purpose of Header guards标题守卫的目的

Question #2问题2

Consider the case where you are using libraries.考虑使用库的情况。 And two libraries would both like to have a function named Push() .两个库都希望有一个名为Push()的函数。 We could name them: LibraryFoo_Push() , and LibraryBar_Push() , or we could use namespaces.我们可以将它们命名为: LibraryFoo_Push()LibraryBar_Push() ,或者我们可以使用命名空间。 Namespaces have some additional benefits with lookups as well.命名空间在查找方面也有一些额外的好处。

And see C++ When is it OK to extend the `std` namespace?并参见C++ 何时可以扩展 `std` 命名空间? for when you are allowed to add stuff to the std namespace.当您被允许向std命名空间添加内容时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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