mercredi 27 mai 2015

Circular includes due to enum within a class in C++

I have an interesting variant of the oft-repeated C++ circular include dilemma. In short: I want to use an enum that is defined in a subclass as the type of an instance variable in a parent class. Here's a minimal example:

a.h

#ifndef A_H
#define A_H

#include "c.h"

class ClassA {
 public:
        ClassA() { };
        void setStatus(ClassC::status status) { a_status = status; }
        void setA(int val) { a_var = val; }
        int valueA() { return a_var; }
        ClassC::status status() { return a_status; }
 private:
        int a_var;
        ClassC::status a_status;
};

#endif // A_H

c.h

#ifndef C_H
#define C_H

#include "a.h"

class ClassC: public ClassA
{
 public:
        enum Status {
                Valid,
                Invalid
        };

        ClassC(): ClassA() { };
        void setC(int val) { c_var = val; }
        int valueC() { return c_var; }
 private:
        int c_var;
};

#endif // C_H

main.cpp

#include "a.h"

int main(int argc, const char *argv[])
{
        ClassA *obj = new ClassA();
        return 0;
}

I can't forward declare the enum in a.h because the name of the enum has to be simple, and ClassC::Status isn't. It's important that the enum be within the namespace of the child class, because there are lots of such classes, and I need to isolate the names of the enum items from values in other enumcs. Any ideas?

Aucun commentaire:

Enregistrer un commentaire