Search code examples
c++inheritanceenumerator

Is it possible to forward declare a enum class to be used in a derived class?


I'm currently making a small little parser for this simple GUI scripting language that I'm creating. Everything works fine, but I need to know if it's possible to do this:

Parser.hpp:

class Parser
{
public:
    enum class LineType;
}

GUIParser.hpp:

class GUIParser : public Parser
{
public:
        enum class LineType
        {
            BACKGROUND,
            BUTTON,
            LABEL,
            RADIOBOX,
            COMMENT
        };
}

This gives me an error, but if it's possible then what syntactical error am I making?

Thanks for any and all help!


Solution

  • That's declaring that there's an enum called LineType inside Parser or: Parser::LineType .

    In the derived class you have an enum called LineType, and its full 'name' will be: GUIParser::LineType.

    So, because you can't predict the name of the derived class, you can't forward declare what it will contain.

    That's the logic behind it, the simpler answer is: no, it's not in the standard.