单个方法 c++ 的命名空间

发布于 2024-10-08 14:21:49 字数 855 浏览 1 评论 0原文

假设我有一个头文件 Snake.h:

#include "SnakeBodyPart.h"
#include "GUI.h"
//...

而 SnakeBodyPart.h 是(除其他外):

#include "GUI.h"
class SnakeBodyPart {
    private :
        GUI::Orientation orientation;
}

最后 GUI.h:

class GUI {
    enum Orientation { NORTH, EAST, SOUTH, WEST };
}

现在在 Snake.cpp 中我想做以下事情:

void Snake::turn(){
    if(bodyPart.getOrientation() == GUI::EAST){
        //do something
    else if (bodyPart.getOrientation() == GUI::SOUTH){
        //do something
    else if ...
}

我想你明白了。我想知道的是:是否可以为单个方法设置命名空间?就像将命名空间 GUI 赋予 Snake::turn 一样,这样我就可以输入 bodyPart.getOrientation() == EAST 吗?

我在 Snake.cpp 中有一些具有不同名称空间的枚举,并且希望通过为某些方法提供某些名称空间(而不仅仅是 Snake.cpp 的一个名称空间)来使代码更具可读性。这可能吗?

Let's say i have a header file Snake.h:

#include "SnakeBodyPart.h"
#include "GUI.h"
//...

And SnakeBodyPart.h is (among other things) :

#include "GUI.h"
class SnakeBodyPart {
    private :
        GUI::Orientation orientation;
}

And at last GUI.h :

class GUI {
    enum Orientation { NORTH, EAST, SOUTH, WEST };
}

Now in Snake.cpp i want to do the following:

void Snake::turn(){
    if(bodyPart.getOrientation() == GUI::EAST){
        //do something
    else if (bodyPart.getOrientation() == GUI::SOUTH){
        //do something
    else if ...
}

I think you get the point. What i would like to know is: is it possible to set a namespace for a single method? Like giving the namespace GUI to Snake::turn, so i can just type bodyPart.getOrientation() == EAST ?

I'm having a few enums with different namespaces in Snake.cpp and would like to make the code more readible, by giving certain methods certain namespaces, not just one namespace for Snake.cpp. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪眸﹌ 2024-10-15 14:21:49

请注意,在您的示例中,GUI 不是命名空间而是类。

话虽这么说,在 turn() 中,您可以编写 using namespace GUI; 来访问 GUI 命名空间中的所有标识符,而无需显式限定它们。或者,您也可以使用 using GUI::EAST; 导入单个符号:

void Snake::turn(){
    using namespace GUI;
    if(bodyPart.getOrientation() == EAST){
        //do something
    else if (bodyPart.getOrientation() == SOUTH){
        //do something
    else if ...
}

Please note that in your example, GUI is not a namespace but a class.

That being said, inside turn() you can write using namespace GUI; to access all identifiers in the GUI namespace without explicitely qualifying them. Alternatively, you can import single symbols using e.g. using GUI::EAST;, too:

void Snake::turn(){
    using namespace GUI;
    if(bodyPart.getOrientation() == EAST){
        //do something
    else if (bodyPart.getOrientation() == SOUTH){
        //do something
    else if ...
}
农村范ル 2024-10-15 14:21:49

由于 GUI 是一个类,因此只需清晰明确地输入 GUI:: 即可,这样每个阅读代码的人都知道正在发生的事情的上下文。您只需编写一次代码并进行一些额外的输入,但人们可能需要阅读代码多年。

编辑:或者,如果 GUI 不需要成为一个类(仅枚举),请将其更改为命名空间并执行 using namespace GUI; 在你的函数中。

Since GUI is a class, just be clear and explicit and type out GUI:: so everyone reading the code knows the context of what's happening. You have to write the code just once with a little extra typing, but people may have to read the code for years.

EDIT: Alternately if GUI doesn't need to be a class (enums only), change it to a namespace and do using namespace GUI; in your function.

你是年少的欢喜 2024-10-15 14:21:49

如果您想要的只是简短的人类可读代码,而不是命名空间创建的实际解析,您可以使用 typedef 或宏来实现所需的结果。

或者,您不需要将枚举包含在类或命名空间中,除非您担心命名冲突,在这种情况下,包含标头就足以创建方向变量或直接引用枚举的方向成员。

If what you want is simply short human readable code and not actual resolution created by a namespace you can use a typedef or macro to achieve the desired results.

Alternately you do not need to contain your enum within a class or namespace unless you are worried about naming conflicts in which case including the header would be enough to be able to create an Orientation variable or reference the directional members of the enum directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文