MQL OOP - Cross references between classes.

 

Hello,

How to make two classes in a way that each class to have a reference to the other? In general I want to have later these classes in separate files.
The problem is that when I define the classes, the first class doesn't see the second definition. 

#property strict

class Foo
  {
public:
                    ~Foo(void) {MyBar=NUL;}

   Bar              *MyBar;
   void SayFoo(void) {Print("It's Foo");}
   void SayBar(void) {MyBar.SayBar();}
  };

class Bar
  {
public:
                    ~Bar(void) {MyFoo=NULL;}

   Foo              *MyFoo;
   void SayBar(void) {Print("It's Bar");}
   void SayFoo(void) {MyFoo.SayFoo();}
  };

int OnInit(void)
  {
   Foo *foo = new Foo();
   Bar *bar = new Bar();

   foo.MyBar = bar;
   bar.MyFoo = foo;

   foo.SayBar();
   bar.SayFoo();

   delete(foo);
   delete(bar);

   return (0);
  }
 
Miroslav Popov:

Hello,

How to make two classes in a way that each class to have a reference to the other? In general I want to have later these classes in separate files.
The problem is that when I define the classes, the first class doesn't see the second definition. 

Not sure if the splitting into files makes it different, but I have been using forward declaration

class Bar;
class Foo  {

   Bar              *MyBar;
... 

 

Thank you Ovo,

It solves the problem.  I must have missed it.