C ++中的全局枚举

时间:2014-07-17 21:20:52

标签: c++ enums

好吧我想将地形枚举定义为

enum terrain {MOUNTAIN, GRASS};

或其他什么。

我如何使这个枚举在我的项目中的所有类中定义?

1 个答案:

答案 0 :(得分:5)

将您的enum声明放在标题文件中:

terrain.h

#ifndef TERRAIN_H
#define TERRAIN_H

enum terrain {MOUNTAIN, GRASS};

#endif

#ifndef / #define对是 include guard ,你可以在其他地方阅读。)

source.cpp

#include <stdio.h>
#include "terrain.h"

// your code here

terrain.h文件包含在您需要的每个源文件中。如果需要,您还可以包含来自另一个头文件的标题。