1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #ifndef A_H_ #define A_H_ class B; // forward declare B class A { public : A(B* b); void trigger(); void execute(); virtual ~A(); private : B* bptr; }; #endif /* A_H_ */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include "A.h" // make sure to include A #include "B.h" #include <iostream> using namespace std; A::A(B* b) : bptr(b) { } void A::trigger() { bptr->executeA( this ); } void A::execute() { cout << "Hello" << endl; } A::~A() { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | #ifndef B_H_ #define B_H_ #include "A.h" class B { public : B(); void executeA(A* a); virtual ~B(); }; #endif /* B_H_ */ |
1 2 3 4 5 6 7 8 9 10 11 | #include "B.h" B::B() { } void B::executeA(A* a) { a->execute(); } B::~B() { } |
1 2 3 4 5 6 7 8 9 | #include "A.h" #include "B.h" using namespace std; int main() { B b; A a(&b); a.trigger(); } |
You saved my life! Thanks man.
ReplyDelete