什么是类的命名空间的好选择? [英] What is a good alternative to namespaces within classes?

查看:122
本文介绍了什么是类的命名空间的好选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是C ++不允许类内的命名空间声明(我在互联网上搜索,发现这个;如果不正确,请说)所以,什么是最好的方式来欺骗这个问题?

The problem is C++ does not allow namespace declarations within classes.(I searched on internet and found this; if it's not true, please say it) So, what's the best way to cheat this problem?

上下文:我的类有一个枚举。

Context: My class have an enumeration within it.

class GameCharacter {
public:
    enum MovingState {
        Running,
        Walking,
        Stopped
    };
    ...
};

OBS:这个例子不是真的,完全是假设的。

OBS: This example is not real, it's totally hypothetical.

C ++定义枚举名称在类范围内,然后使用这些状态我必须直接从类名使用scope操作符( GameCharacter :: Running ,或者使用GameCharacter :: Running

C++ defines that the enumeration names are inside the class scope, then to use these states I have to use the scope operator directly from the class name(GameCharacter::Running, for instance; or using GameCharacter::Running).

我认为这是坏的,因为属于枚举的名称在类范围内;我想要有一个适用于RunningState枚举的范围。 (以这种方式访问​​: GameCharacter :: MovingState :: Running
那么,我的第一个想法是创建一个命名空间,它将定义枚举的范围

I think this is bad, because the name which belongs to the enumeration is inside the class scope; I wanted to have a scope for the RunningState enumeration. (accessing it this way: GameCharacter::MovingState::Running) My first thought, then, was to create a namespace which would define a scope for the enumeration.

class GameCharacter {
public:
    // does not compile
    namespace MovingState {
        enum State {
            Running,
            Walking,
            Stopped
        };
    };
    ...
};

但是C ++禁止它。这段代码没有编译。 ( main.cpp:3:5:错误:''命名空间'之前的未标准化标识符

But C++ forbids it. This code does not compile. (main.cpp:3:5: error: expected unqualified-id before ‘namespace’)

为什么我以这种方式做事情是因为有可能创建一个具有相同范围的名称的第二个枚举。这可能会导致冲突的名称。

The reason why I'm trying to do things this way is because there is a possibility to create an second enumeration with names in same scope. Which could cause conflicting names.

class GameCharacter {
public:
    enum MovingState {
        Running,
        Walking,
        Stopped
    };
    enum DrivingState {
        Accelerating,
        Breaking,
        Stopped        // Compilation error: conflicts with previous declaration ‘GameCharacter::MovingState GameCharacter::Stopped’
    };
    ...
};

(我的想法是,在这种情况下,州应该被称为 GameCharacter :: MovingState :: Stopped GameCharacter :: DrivingState :: Stopped
那么该怎么办? p>

(my idea was that, in this case, the states should be referred as GameCharacter::MovingState::Stopped and GameCharacter::DrivingState::Stopped) Then, what should I do?

推荐答案

在C ++ 1x(去年标准化的新版本的C ++)中,您可以使用强类型枚举,它们将标识符放在枚举类本身的范围内(除其他改进之外) 。这些被声明为枚举类

In C++1x (the new version of C++ that was standardised last year), you can use strongly typed enumerations, which put the identifiers in the scope of the enum class itself (among other improvements). These are declared with enum class:

class GameCharacter {
public:
    enum class State {
        Running,
        Walking,
        Stopped    // Referred to as GameCharacter::MovingState::Stopped.
    };
    ...
};

同时,如果您遇到C ++ 03编译器或想保留兼容性,只需使用 class 而不是命名空间,并声明您在自己的答案中建议的构造函数私有。如果外界不需要查看,您可以将整个私有。

In the meantime, if you're stuck with a C++03 compiler or want to retain compatibility, just use class instead of namespace and declare the constructor private as you suggested in your own answer. You can make the entire class private if the outside world doesn't need to see it.

这篇关于什么是类的命名空间的好选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆