Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving circular dependency in which each dependent structure accesses it's methods

Tags:

c++

How should I resolve the following type of circular dependency?

//A.hpp
#include "B.hpp"

struct A {
    B b;
    int foo();
};

//A.cpp
#include "A.hpp"

int A::foo{
    b.fi(*this);
}


//B.hpp
struct A;

struct B {
    int fi(const A &a);
};

//B.cpp
#include "B.hpp"

int B::fi(const A &a){
    if(a.something()) 
        something_else();
}
like image 861
MVTC Avatar asked Jun 16 '13 02:06

MVTC


1 Answers

Forward declare A in B.hpp as you have,, then include A.hpp in B.cpp. That's what forward declarations are for.

like image 93
Chad Avatar answered Nov 15 '22 03:11

Chad