在 C++ ,“_MOVE_H”有什么特别之处?
我有一个像这样的 C++ 文件
#ifndef _MOVE_H
#define _MOVE_H
class Move {
int x, y;
public:
Move(int initX = 0, int initY = 0) : x(initX), y(initY) {}
int getX() { return x; }
void setX(int newX) { x = newX; }
int getY() { return y; }
void setY(int newY) { y = newY; }
};
#endif
令我惊讶的是,编译器会忽略 #ifndef
和 #endif
之间的所有代码(我发誓我没有定义< code>_MOVE_H 任何其他地方),并且我有各种关于缺少定义的错误。我以为我做错了什么,但是当我尝试使用另一个键(例如 _MOVE_Ha
)时,一切都恢复正常。_MOVE_H
在 C++ 中意味着什么特殊的东西吗?
我我正在运行 Ubuntu 10.04、GCC 4.4.3,如果有的话,
谢谢。
I have a C++ file like this
#ifndef _MOVE_H
#define _MOVE_H
class Move {
int x, y;
public:
Move(int initX = 0, int initY = 0) : x(initX), y(initY) {}
int getX() { return x; }
void setX(int newX) { x = newX; }
int getY() { return y; }
void setY(int newY) { y = newY; }
};
#endif
And to my amazement, all the code between #ifndef
and #endif
is simply ignored by the compiler (I swear that I am not defining _MOVE_H
anywhere else), and I have all kinds of errors about missing definitions. I was thinking that I did something wrong, but when I try to use another key (like _MOVE_Ha
, everything is back to normal. Does _MOVE_H
mean something special in C++ ?
I'm running Ubuntu 10.04, GCC 4.4.3, if that matters.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是防止同一头文件被多次包含的技巧。 #define 的实际值并不重要 - 只要它仅在该头文件中定义,约定是大写的 NAME_HEADER_FILE_H
另请参阅 #pragma Once
It's a trick to prevent the same header file being included more than once. The actual value you #define doesn't matter - so long as it's only defined in that header file, the convention is NAME_HEADER_FILE_H in capitals
See also this discussion on #pragma once
任何以下划线开头然后大写字母的内容都保留给实现。 (即
_M
)。我认为一般来说你想要远离前导下划线。Anything beginning with an underscore then capital letter is reserved to the implementation. (i.e.
_M
). I think in general you want to stay away from leading underscores.我相信 gcc 有一个名为 move.h 的包含文件,其中包含哨兵 _MOVE_H。想必你已经遇到过这个问题。使用不同的标识符,最好是不以下划线开头的标识符。我在我的里面放了一个 GUID,但后来我真的着迷了:-)
I believe gcc has an include file called move.h that includes the sentinel _MOVE_H. Presumably you have collided with this. Use a different identifier, preferably one that doesn't start with an underscore. I put a GUID in mine, but then I'm really obsessive :-)
只需在您的计算机上的 /usr/include/c++ 中运行 grep _MOVE_H 即可
:
根据经验,不要使用以
_
为前缀的 things (实际上是任何东西)或<代码>__。它保留供内部使用。使用
SOMETHING_MOVE_H
(通常是公司名称,...)。我猜这是一个新的标头,用于将移动语义添加到 c++0x。
just run grep _MOVE_H in /usr/include/c++ on your machine
for me :
As a rule of thumb, don't use things (really anything) prefixed by
_
or__
. It's reserved for internal usage.Use
SOMETHING_MOVE_H
(usually name of the company, ...).I guess it's a new header used to add the move semantic to c++0x.