Download MetaTrader 5
To add comments, please log in or register
Sell products via the Market and track your sales statistics
estados
15
estados 2014.04.04 12:24 

Hello everyone. Sorry for my English.

This code gives me compile error.

'Class2' - declaration without type Class1.mqh

class Class1
{
   private:
      Class2         class2;
   public:
                     Class1(){};
                    ~Class1(){};
};


class Class2
{
   private:
      Class1         class1;
   public:
                     Class2(){};
                    ~Class2(){};
};


I need to communicate two classes because the class one has elements that need class two and one class, need to access the elements of class two.

Not possible in MQL4, make cross-references between classes?

I do not understand. Any help?. Thanks

Alain Verleyen
Moderator
28670
Alain Verleyen 2014.04.04 12:54  
estados:

Hello everyone. Sorry for my English.

This code gives me compile error.

'Class2' - declaration without type Class1.mqh


I need to communicate two classes because the class one has elements that need class two and one class, need to access the elements of class two.

Not possible in MQL4, make cross-references between classes?

I do not understand. Any help?. Thanks

class Class2;

class Class1
{
   private:
      Class2         *class2;
   public:
                     Class1(){};
                    ~Class1(){};
};


class Class2
{
   private:
      Class1         *class1;
   public:
                     Class2(){};
                    ~Class2(){};
};
estados
15
estados 2014.04.04 13:25  

Thank you very much angevoyageur. Testing, I found that if used includes the declaration of the class should come first, that no error.

class Class2;

#include <Class2.mqh>

class Class1;

#include <Class1.mqh>
whroeder1
13644
whroeder1 2014.04.04 15:29  
If you have mutually communicating classes like that, it should be redone. In a court the defendant and plaintive don't talk to each other, they talk to the judge. Put your two classes inside a controlling class and eliminate the dependency.
GreenMoney
379
GreenMoney 2014.04.04 17:00  

@WHRoeder

Just a thought...what about creating a base class with static class members (variables) and then create an new class derived from the base class to doing whatever needs to be done? Wouldn't that give the derived class the ability to both see and change the inherited data members of the base class? Moreover, wouldn't that also give all classes derived from the base class the ability to see (and, if needed, change--maybe depending on the abilities of the derived class) the data contained in the variables of the base class?

To be honest, while I'm not quite new to concepts of OOP, it has been a few years since I have implemented them, so like allot of people here, I'm attempting to refresh myself. :)

Alain Verleyen
Moderator
28670
Alain Verleyen 2014.04.04 18:10  

@WHRoeder, @Thirteen

I gave an answer to the opening poster to resolve his issue, but I agree with you. I can't think a situation where such design is the best solution and in my opinion it's certainly a bad practice to work this way. Though it could be interesting to hear @estados about the purpose of his implementation.

estados
15
estados 2014.04.04 18:52  
Really as you say, it's like I'm doing. Try to simplify the technical problem I had, to be easier to understand.
whroeder1
13644
/
To add comments, please log in or register