Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ circular dependencies with nested class

I'm trying to generate header files of classes I'm reconstructing from what I disassembled with IDA. However I'm getting compile errors due to circular dependencies. For regular classes I solved it by declaring them in a separate file I include as a first. The thing is I cannot declare inner class without definition of an outer class which is the problem.

An example class structure:

Class A:

#include "B.h"

class A {

public:
    class Nested {
    public:
        void foo(B::Nested &foo);
    };
};

Class B:

#include "A.h"

class B {

public:
    class Nested {
    public:
        void foo(A::Nested &foo);
    };
};
like image 701
Honza Bednář Avatar asked Mar 03 '23 17:03

Honza Bednář


1 Answers

You can forward declare the Nesteds in A and B, and define them afterward.

a.h

class A {
public:
    class Nested;
};

B.h

class B {
public:
    class Nested;
};

Nested.h

#include "A.h"
#include "B.h"

class A::Nested {
public:
    void foo(B::Nested &foo);
};

class B::Nested {
public:
    void foo(A::Nested &foo);
};
like image 68
Caleth Avatar answered Mar 12 '23 10:03

Caleth