Download MetaTrader 5

Calling Methods of Nested Classes - HowTo?

To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
Carl Schreiber
6586
Carl Schreiber 2016.09.14 20:11 

Hi,

the problem seems to be easy but I am stuck.

I want to have 2 classes CQ and CI.

CI in its constructor searches within an array of pointers (ArrCQ[]) to existing instances of CQ whether there is one that matches.

For this "match-check" CQ has a method match(..) but how can I call from within the CI-constructor the CQ method match?

This try causes a compiler error: 'match' - function not defined.

In addition to that if within the loop no match have been found a new instance of CQ should be created and the pointer should be inserted into the (increased) array. So I need to call as CQs contructor with the array and th two IDs.


// first Class - File
property strict

#include <Object.mqh>
class CQ                      // forward declaration
class CI : public CObject {
   private: 
      CQ*      pCQ;
      double   arrVal[];
      
   protected: 
      // I need a pointer to the public array arr[] of the class Q
      // I need to access the public method of
   public
      int    oID1_CI, oID2_CI;
      CI(CQ* &arrCQ[], const double id1, const double id2); // Constructor
      ~CI();
}
CI::CI(CQ* &arrCQ[], const double id1, const double id2) {
      oID1_CI = id1;  //store the values
      oID2_CI = id2;
      // ok go and find the correct instance of CQ:
      int iQ = ArraySize(arrCQ); // array of pointers to existing instances of CQ
      while (iQ-->0) {
         if (arrCQ[iQ]::match(id1,id2)) { // <==  COMPILER ERROR How do I call CQs method here?
            pCQ = arrCQ[iQ];
            return;
         }
      }
      // in case of no match
      // incease the array by 1 and create 
      // create a new instance of CQ
      ...
      arrCQ[newIdx] = new CQ(arrCQ, id1, id2);
       
}
// ################   end   #########################

// second class file
property strict

#include <Object.mqh>
class CI                      // forward declaration
class CQ : public CObject {
   private:
         // need an array of pointers to all instances of I
         CI* arrCI[];
   public:
         int    oID1_CQ, oID2_CQ;
         double arr[];
         
              CQ(CQ* &arrCQ[], const int idx, const double d1, const double d2); // Constructor
              ~CQ();
         bool match(const int id1, const id2) { return(id1==oID1_CQ && id2==oID2_CQ); }
}
Alain Verleyen
Moderator
30721
Alain Verleyen 2016.09.14 21:12  

Seems trivial, maybe I misunderstood : with a dot like all methods of objects.

if (arrCQ[iQ].match(id1,id2)) {
...
}
Carl Schreiber
6586
Carl Schreiber 2016.09.14 21:51  

Thank you - at the first glance it seems to be perfect the . instead of :: removes the compiler error  - but (sorry) after further google search I found:

". for accessing object member variables and methods via object instance"

and

"-> for accessing object member variables and methods via pointer to object"

so trying to use -> as it s an array of pointer to instances ( CQ* &arrCQ[] ) causes as well a compiler error.

I am not that far to test my example but this raises the question behaves OOP of MQL5 different than C++ (from stackoverflow)?

1.=> -> for accessing object member variables and methods via pointer to object
Foo *foo = new Foo();
foo->member_var = 10;
foo->member_func();

2.=> . for accessing object member variables and methods via object instance
Foo foo;
foo.member_var = 10;
foo.member_func();

3.=> :: for accessing static variables and methods of a class/struct or namespace. 
It can also be used to access variables and functions from another scope 
(actually class, struct, namespace are scopes in that case)

Why does -> not work?

Wouldn't I get an error if at runtime the . is used for a pointer?

Any thanks so far from an OOP newbee!

What is the difference between "::" "." and "-&gt;" in c++
What is the difference between "::" "." and "-&gt;" in c++
  • stackoverflow.com
I have created the class called Kwadrat and I have three int fields inside. The Code Blocks gives me advice that i can get into the field of the object by , and . The arrow is the one that only works, but why? What's the difference between those three?
Stanislav Korotky
17876
Stanislav Korotky 2016.09.14 22:17  
MQL is not C++. It does not have -> operator. It uses . both for pointers and references.
Carl Schreiber
6586
Carl Schreiber 2016.09.14 22:26  
Stanislav Korotky:
MQL is not C++. It does not have -> operator. It uses . both for pointers and references.
Ahh - ok! Thank you!
To add comments, please log in or register