pointers to classes

 

I am not sure if I am in the right area..  

Thank you for the responses I was hoping i could do it in separate class's so I could modefily the one program to be used with different subprograms ..

I will just have to use the cut and paste method if that works  I will be  very greatful thank you

I need help.. with headers and compiling.

I have  2 classes..

class A.. has a pointer to class C


class C has a pointer to class A


basically   class A is a link list program and I want it to point to class C


and Class C is  a data  class that holds the data and  obviously does this using  Class A so it needs  Class A to be compiled properly..


I need to know if there is a way to get this to compile or am I kicking a DEAD horse...  if you can help with a suggestion I would appreciate the help

 

Both classes have to be in the same file (e.g. CA.mqh)!

Like:

class C;
class A { protected: C* RefToC; ... };
class C { protected: A* RefToA; ... };
// and now all the various methods of both classes
...
 
Eadbhard:

I am not sure if I am in the right area..  

I need help.. with headers and compiling.

I have  2 classes..

class A.. has a pointer to class C


class C has a pointer to class A


basically   class A is a link list program and I want it to point to class C


and Class C is  a data  class that holds the data and  obviously does this using  Class A so it needs  Class A to be compiled properly..


I need to know if there is a way to get this to compile or am I kicking a DEAD horse...  if you can help with a suggestion I would appreciate the help

What does the data class need from the collection class (why does it need its pointer)?

*-It can be done like Carl Schreiber points out, but usually it is a design flaw. Better to ask that question first.

**-And they don't have to be in the same fille, you can for file class A

class C;
class A
{
private:
C       *objc;  
};

and for the file of class C

class A;
class C
{
private:
A       *obja;
};
 

Well it was my experience that the MT-compiler won't take the cross reference if the classes are in two files.

So I have them in one file (and never change it) - but try it yourself!

 
Whatever 2 classes that can be coded in one file can be declared in 2 files with prompt class forward declaration and the use of class pointers
 

This. There are no issues splitting this pattern across src files. Here is an example project I made for reference. 


worker.mqh

#include <arrays\arrayobj.mqh>

template<typename T>
class objvector : public CArrayObj
{
 public:
   T operator[](const int i) const { return this.At(i); }
   bool Add(T element){ return CArrayObj::Add(element); }
};

class Manager;

class Worker : public CObject
{
   Manager             *m_manager;
   objvector<Worker*>   m_colleagues;
   int                  m_id;
public:
                        Worker(Manager *mgr, int id):m_manager(mgr),
                                                     m_id(id){}
   string               symbol() const { return m_manager.symbol(); }
   int                  id()     const { return m_id; }
   objvector<Worker*>*  colleagues();
};

objvector<Worker*>* Worker::colleagues() 
{ 
   objvector<Worker*>* workers = m_manager.workers();
   int mgr_total = workers.Total();
   int col_total = m_colleagues.Total();
   if(mgr_total < 2 || mgr_total -1 == m_colleagues.Total())
      return &m_colleagues;
   m_colleagues.Clear();
   for(int i=0; i<mgr_total; i++) 
      if(workers[i] != &this)
         m_colleagues.Add(workers[i]);
   return &m_colleagues;
}


mgr.mqh

#include "worker.mqh"


class Manager {
   string               m_symbol;
   int                  m_timeframe;
   objvector<Worker*>   m_workers;
public:
                        Manager(string s, int tf):m_symbol(s),
                                                  m_timeframe(tf){}
   void                 add_worker(int id);
   string               symbol()    const { return m_symbol; }
   void                 symbol(string s)  { m_symbol = s; }
   int                  timeframe() const { return m_timeframe; }
   objvector<Worker*>*  workers() { return &m_workers; }
};

void Manager::add_worker(int id)
{
   Worker *worker = new Worker(&this, id);
   m_workers.Add(worker);
}

TestScript.mq4

#include "mgr.mqh"

void OnStart()
{
//---
   Manager mgr(_Symbol, _Period);
   
   for(int i=0; i<5; i++)
      mgr.add_worker(i);
      
   Worker *zero = mgr.workers()[0]; //getting a worker from manager
   //Worker *one = zero.colleagues()[0];//access to colleagues directly from worker via manager proxy
   
   printf("I am worker %d and I have %d colleagues.", 
      zero.id(), zero.colleagues().Total()
   );
   printf("I am a colleague of worker %d and my id is %d",
      zero.id(), zero.colleagues()[0].id()
   );
   printf("assert workers access to manager via colleagues");
   printf("%s == %s",
      zero.symbol(), zero.colleagues()[0].symbol()
   );
   printf("Manager state change to USDJPY");
   
   mgr.symbol("USDJPY");
   
   printf("%s == %s",
      zero.symbol(), zero.colleagues()[0].symbol()
   );
}
Files:
MgrWrkr.zip  17 kb