Features of the mql5 language, subtleties and tricks - page 245

 

The question in the picture is:

code:
class C_AO_Agent
{
public:
    double c[];
    double f;
};

class C_AO
{
public:
    double cB[];
     C_AO_Agent *a;

    virtual void Init() { }
};

class C_Agent : public C_AO_Agent
{
public:
    double g;
    
    class ad
    {
    
    };
};

class C_P : public C_AO
{
public:
    void Init()
    {
      a = new C_Agent;
      a.f = 5;
    }
};

void OnStart()
{
    C_AO *ao;
    ao = new C_P;
    ao.Init();
}
 
Andrey Dik #:

The question in the picture is:

code:

Forum on trading, automated trading systems and testing trading strategies.

Features of mql5 language, subtleties and techniques of work

Andrey Dik, 2024.02.16 06:05

class C_AO_Agent
{
public:
    double c[];
    double f;
};

class C_AO
{
public:
    double cB[];
     C_AO_Agent *a;

    virtual void Init() { }
};

class C_Agent : public C_AO_Agent
{
public:
    double g;
    
    class ad
    {
    
    };
};

class C_P : public C_AO
{
public:
    void Init()
    {
      a = new C_Agent;
      a.f = 5;
    }
};

void OnStart()
{
    C_AO *ao;
    ao = new C_P;
    ao.Init();
}

The base class does not have access to the fields of descendants.

 
fxsaber #:

The base class does not have access to descendant fields.

Yes, but I defeated it this way:

class C_AO_Agent
{
public:
    double c;
    double f;
};

class C_AO
{
public:
    double cB[];
     C_AO_Agent *a;

    virtual void Init() { }
};

class C_Agent : public C_AO_Agent
{
public:
    double g;
    
    class ad
    {
    
    };
};

class C_P : public C_AO
{
public:
    void Init()
    {
      a = new C_Agent;
      C_Agent *agent = (C_Agent*)a; // Приведение типов
      agent.f  = 5;
      agent.g  = 7;                 //теперь есть доступ к g!!!
      agent.c  = 8;
    }
};

void OnStart()
{
    C_AO *ao;
    ao = new C_P;
    ao.Init();
    
    //тут нам нужен доступ только к f и c
    Print (ao.a.f);
    Print (ao.a.c);
    
    // Удаление объектов
    delete ((C_Agent*)ao.a);
    delete ao;
}
this is a working version of what was required.
 
Andrey Dik #:
yes, but defeated this way:

is a working version of what was required.
It's very buggy.
In the OnStart method you should specify a type that is known only in the Init method of the C_P class.
I think it is better to add Deinit() for C_P.
 
Sergey Gridnev #:
It's very buggy.
In the OnStart method it is necessary to specify the type that is known only in the Init method of the C_P class.
I think it is better to add Deinit() for C_P.
Show me an example, and anyone can offend an artist)
 
Andrey Dik #:
Show me an example
class C_AO_Agent
{
public:
    double c;
    double f;
};

class C_AO
{
public:
    double cB[];
     C_AO_Agent *a;

  C_AO( void ) : a(NULL) {}
  
  ~C_AO( void )
  {
    this.Init();
  }

    virtual void Init()
    {
      if (::CheckPointer(this.a) == POINTER_DYNAMIC)
        delete this.a;
    }
};

class C_P : public C_AO
{
protected:
  class C_Agent : public C_AO_Agent
  {
  public:
      double g;
      
      class ad
      {
      
      };
  } *agent;
  
public:
    C_P( void ) : agent(NULL) {}
    
    virtual void Init()
    {
      this.C_AO::Init();
            
      this.agent = new C_Agent;
      this.a = this.agent;
    }
};

void OnStart()
{
    C_AO *ao;
    ao = new C_P;
    ao.Init();
    
    //тут нам нужен доступ только к f и c
    Print (ao.a.f);
    Print (ao.a.c);
    
    // Удаление объектов
    delete ao;
}
 
Andrey Dik #:
Show an example, and anyone can offend an artist).
Example of what?
How to add a void Deinit(void) method to a class that calls delete(a) if a is a valid pointer to an object?
Or how to write ao.Deinit() in OnStart()?
;)
 
Sergey Gridnev #:
Example of what?
How do you add a void Deinit(void) method to a class that calls delete(a) if a is a valid pointer to an object?
Or how to write ao.Deinit() in OnStart()?
;)

The task was to get access to an object, not to organise correct deleting of objects. Deleting is always easier than creating))

 
fxsaber #:
Thanks, that's interesting.
 

if you move the mouse pointer over the name of a variable or function and press Ctrl, you move to the definition or declaration of this variable (function).
This is implemented in almost all IDEs, only when Ctrl is pressed, the variable is highlighted and underlined. It is not in ME.


Ihave not found this feature in the help.
The same thing can be achieved by pressing Alt+G, but it is still more convenient to use the mouse.