C++ enum forward declaration

·

1 min read

Preface

There are two forms of enumeration in C++, enum and enum class. Enum is the pre-c++11 syntax, which means it is an unscoped enumeration. The enum class was introduced in C++11 and provides a scoped enumeration

Unscoped enumerations

// A.h
enum Type : unsigned int; // Forward declaration

class A {
public:
    A();
    ~A();
    Type mType;
};

// A.cpp
#include "A.h"
#include "B.h"
...

// B.h
enum Type : unsigned int
{
    TEST_TYPE_A,
    TEST_TYPE_B
};

class B {
public:
    B();
    ~B();
};

Scoped enumerations

// A.h
enum class Type : unsigned int; // Forward declaration

class A {
public:
    A();
    ~A();
    Type mType;
};

// A.cpp
#include "A.h"
#include "B.h"
...

// B.h
enum class Type : unsigned int
{
    TEST_TYPE_A,
    TEST_TYPE_B
};

class B {
public:
    B();
    ~B();
};

Reference

https://en.cppreference.com/w/cpp/language/enum