samedi 2 avril 2022

C++ circular header includes - how can I solve this type? [duplicate]

I implemented a visitor design pattern. I want to eliminate the circular header includes. The code above is working, g++ compile it.

I have a file: Classes.h It has base and derived class declaration, also has Classes.cpp with the definitions of methods.

Classes.h:

#include "VisitorDesignPattern/Visitor.h"

class Visitor;
class DerivedVisitorA;
class DerivedVisitorB;

class Base
{
    public:
       ...
       virtual void accept(Visitor& v) = 0;
};

class DerivedA : public Base
{
    public:
        void accept(Visitor &v);
};

class DerivedB : public Base
{
    public:
        void accept(Visitor &v);
};

Classes.cpp contains the definitions, definitons of accept method, as well, as I mentioned.

Visitor.h:

#include "../Classes.h"

class Base;
class DerivedA;
class DerivedB;

class Visitor
{
    public:
        virtual void Visit(DerivedA &derivedA) = 0;
        virtual void Visit(DerivedB &derivedB) = 0;
};

class DerivedVisitorA : public Visitor
{
    public:
        void Visit(DerivedA &derivedA);
        void Visit(DerivedB &derivedB);
};

class DerivedVisitorB : public Visitor
{
    public:
        void Visit(DerivedA &derivedA);
        void Visit(DerivedB &derivedB);
};

Visitor.cpp contains the definition of methods.

I am using this in every .h file:

#ifndef VISITOR_H
#define VISITOR_H
...
#endif

#ifndef CLASSES_H
#define CLASSES_H
...
#endif

So in this case how can I eliminate the circular header includes?

Aucun commentaire:

Enregistrer un commentaire