C++ 命名空间和在单独的文件中定义类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4093407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:31:53  来源:igfitidea点击:

C++ namespaces and defining classes in separate files

c++fileheadernamespaces

提问by guitar-

I want to make a namespace that will contain several classes as part of a "package".

我想创建一个包含多个类作为“包”的一部分的命名空间。

Do I have to declare all of the classes within the namespace?

我是否必须声明命名空间中的所有类?

For example, if I have a "2dEngine.h" which defines the 2dEngine namespace, do I have to declare all of the individual classes within that header file? Or can I still separate them into separate header (.h) files and have them be part of the namespace?

例如,如果我有一个定义 2dEngine 命名空间的“2dEngine.h”,我是否必须声明该头文件中的所有单个类?或者我仍然可以将它们分成单独的头文件 (.h) 并使它们成为命名空间的一部分?

Pseudo example:

伪示例:

TwoEngine.h

双引擎.h

namespace TwoEngine
{
    class Canvas
    {
        // Define all of Canvas here
    };

    class Primitive
    {
        // Define all of Primitive here
    };
}

Instead of doing that, I want to have Canvas and Primitive be their own .h files and just somehow state that they are part of that namespace.

而不是这样做,我想让 Canvas 和 Primitive 成为它们自己的 .h 文件,并以某种方式声明它们是该命名空间的一部分。

Sorry, I'm still pretty new to this.

抱歉,我对此还是很陌生。

回答by Alex B

Yes, you can split the namespace into multiple blocks (and hence files). Your classes will belong to the same namespace as long as they are declared in the namespaceblock with the same name.

是的,您可以将命名空间拆分为多个块(以及文件)。只要它们在namespace具有相同名称的块中声明,您的类将属于相同的命名空间。

// Canvas.h
namespace TwoEngine
{
    class Canvas
    {
        // Define all of Canvas here
    };
}

// Primitive.h
namespace TwoEngine
{
    class Primitive
    {
        // Define all of Primitive here
    };
}

回答by Chubsdad

Namespaces can be discontiguous. You can take advantage of this by keeping relevant classes in your 2DEngine.h which probably is going to be used by client code and will be shipped as part of your library.

命名空间可以是不连续的。您可以通过在 2DEngine.h 中保留相关类来利用这一点,该类可能会被客户端代码使用,并将作为库的一部分提供。

Anything else, that is not to be revealed to the outside world can still be put in the same namespace but in a separate header file (which is not shipped).

其他任何不向外界透露的内容仍然可以放在同一个命名空间中,但放在一个单独的头文件中(未提供)。

Header H1.h (part of the library interface to the external world)

Header H1.h(库对外接口的一部分)

namespace TwoEngine 
{ 
    class Canvas 
    { 
        // Define all of Canvas here 
    }; 
}

Header H2.h (not part of the library interface to the external world)

头文件 H2.h(不是外部世界库接口的一部分)

#include "H1.h"
namespace TwoEngine      // reopen the namespace and extend it
{
    class Primitive 
    { 
        // Define all of Primitive here 
    }; 
}

回答by Preet Sangha

Yes just use the name space directive inside the implementation files also.

是的,也只需在实现文件中使用命名空间指令。