Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global enum in C++

Tags:

c++

enums

Ok say I want to define a terrain enum as

enum terrain {MOUNTAIN, GRASS};

or something.

How would I make this enum something that is defined in all the classes in my project?

like image 262
user2827048 Avatar asked Dec 26 '22 06:12

user2827048


1 Answers

Put your enum declaration in a header file:

terrain.h

#ifndef TERRAIN_H
#define TERRAIN_H

enum terrain {MOUNTAIN, GRASS};

#endif

(The #ifndef/#define pair is an include guard, you can read about those elsewhere.)

source.cpp

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

// your code here

Include the terrain.h file in every source file where you need it. You can also include a header from another header file if you need to.

like image 102
Greg Hewgill Avatar answered Jan 06 '23 20:01

Greg Hewgill