单个方法 c++ 的命名空间
假设我有一个头文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,在您的示例中,
GUI
不是命名空间而是类。话虽这么说,在
turn()
中,您可以编写using namespace GUI;
来访问GUI
命名空间中的所有标识符,而无需显式限定它们。或者,您也可以使用using GUI::EAST;
导入单个符号:Please note that in your example,
GUI
is not a namespace but a class.That being said, inside
turn()
you can writeusing namespace GUI;
to access all identifiers in theGUI
namespace without explicitely qualifying them. Alternatively, you can import single symbols using e.g.using GUI::EAST;
, too:由于
GUI
是一个类,因此只需清晰明确地输入GUI::
即可,这样每个阅读代码的人都知道正在发生的事情的上下文。您只需编写一次代码并进行一些额外的输入,但人们可能需要阅读代码多年。编辑:或者,如果 GUI 不需要成为一个类(仅
枚举
),请将其更改为命名空间并执行using namespace GUI;
在你的函数中。Since
GUI
is a class, just be clear and explicit and type outGUI::
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 (
enum
s only), change it to a namespace and dousing namespace GUI;
in your function.如果您想要的只是简短的人类可读代码,而不是命名空间创建的实际解析,您可以使用 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.