Interface in C++ for cross reference

#include "B.h"

class A {
public:
   virtual ~A() {}
   virtual void onStart() = 0;
   virtual void onEnd() = 0;
private:
   B b;
}
#include "A.h"

class B {
public:
  B (A *a) : ap (a) {}
  void exec()
  {
      ap->onStart();
      //...
      ap->onEnd();
  }
private:
  A *ap;
}

Compile error: invalid use of undefined type struct A

Class IA {
public:
  virtual void onStart() = 0;
  virtual void onEnd() = 0;
}
#include "IA.h"
#include "B.h"

Class A : public IA {
public:
   A();
   ~A();
   void onStart() { ... }
   void onEnd() { ... }
private:
   B b;
};
#include "IA.h"

class B : public IA {
public: 
   B(IA *ip) : iap(ip) {}
   void exec()
   {
       iap->onStart();
       //....
       iap->onEnd();
   }
private:
   IA* iap;
};
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s