简体   繁体   English

将两个或多个 C++ 命名空间合并为一个

[英]Combine two or more C++ namespaces into one

First of all, sorry for my English.首先,对不起我的英语。

Ok, I'm working a program that performs a specific process.好的,我正在编写一个执行特定过程的程序。 This process needs some classes and functions to be defined.这个过程需要定义一些类和函数。 All of them must be organized in blocks to access them.所有这些都必须组织在块中才能访问它们。

My first idea was to work with namespaces (C++), to get something like this:我的第一个想法是使用命名空间(C++),得到这样的东西:

namespace LoadSystem
{
   namespace ParseBlock1
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
   namespace ParseBlock2
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
}

So, I was reading to get the best idea if this is good or not.所以,我正在阅读以获得最好的主意,这是否好。 I already have read that I must not use lots of nested namespaces, so, for my purpose, the minimum levels are two, as shown above.我已经读过我不能使用大量嵌套的命名空间,因此,就我而言,最低级别为两个,如上所示。

My objective is to be able to add more and more ParseBlocks into the LoadSystem namespace.我的目标是能够将越来越多的ParseBlocks添加到LoadSystem命名空间中。 It will be stored in a single .h file, so, there will be only interfaces of the classes.它将存储在单个.h文件中,因此,将只有类的接口。 Since there can be lots of classes per block, I want to split the definition of each block into other .h files to keep the main .h file small as I can.由于每个块可以有很多类,我想将每个块的定义拆分为其他.h文件,以尽可能保持主.h文件较小。

So, I ended with an idea of defining a file block1.h and block2.h , each one with a structure like this:所以,我最后想到了定义一个文件block1.hblock2.h ,每个文件block1.h这样的结构:

namespace LoadSystem
{
   namespace ParseBlock1
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
}

and

namespace LoadSystem
{
   namespace ParseBlock2
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
}

And import them in the load_system.h file.并将它们导入到load_system.h文件中。 So, every time I need to add another block, I write the needed files, and finally, I just import the new blockX.h into the main load_system.h .因此,每次我需要添加另一个块时,我都会编写所需的文件,最后,我只需将新的blockX.h导入主load_system.h

Then, I must be able to access both blocks from the same namespace using LoadSystem::ParseBlock1::Class1 or LoadSystem::ParseBlock2::Class1 .然后,我必须能够使用LoadSystem::ParseBlock1::Class1LoadSystem::ParseBlock2::Class1从同一命名空间访问两个块。

I have tested this with simple integer values and it works.我已经用简单的整数值对此进行了测试,并且可以正常工作。 The namespaces combine and I can access the values without any warning (I used gcc -Wall -Werror -Wextra -pedantic ).命名空间组合在一起,我可以在没有任何警告的情况下访问这些值(我使用了gcc -Wall -Werror -Wextra -pedantic )。

So, is this combination of namespaces is correct or not.那么,这种命名空间的组合是否正确。 Maybe it works, but I maybe should not use it, I don't know.也许它有效,但我也许不应该使用它,我不知道。

Also, I want to know if this process, of importing a "master" header file (which imports other header files) is also correct or not (I'm using the needed #ifndef , #define and #endif macros to guard from multiple imports), I'm using something like this:另外,我想知道导入“主”头文件(导入其他头文件)的这个过程是否也正确(我正在使用所需的#ifndef#define#endif宏来保护多个进口),我使用的是这样的:

# ifndef LOAD_SYSTEM_H_
# define LOAD_SYSTEM_H_

# include "block1/block1.h"
# include "block2/block2.h"

# endif

So, please help me to know if this is correct or not.所以,请帮助我知道这是否正确。

You can always extend an existing namespace, so that part is OK.您始终可以扩展现有的命名空间,因此该部分没问题。

And that part is the only that has a simple technical answer.而那部分是唯一具有简单技术答案的部分。

Regarding "master header file", it's more subjective, a matter of personal preference.关于“主头文件”,比较主观,看个人喜好。 I prefer that the included headers can be included on their own without any prerequisites (like including other stuff before them).我更喜欢包含的标头可以在没有任何先决条件的情况下单独包含(例如在它们之前包含其他内容)。 If so, then all's OK for me, but if not, then users of your code will in practice have to include the big master header file in order to get any little smaller header, and that can affect build times negatively (and if they don't, but themselves include the prerequisites, then they have brittle code that can stop working when you update a header).如果是这样,那么对我来说一切都好,但如果不是,那么您的代码用户实际上必须包含大主头文件以获得任何较小的头文件,这可能会对构建时间产生负面影响(如果他们不这样做) 't,但它们本身包含先决条件,然后它们具有脆弱的代码,当您更新标题时可能会停止工作)。

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

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