Download MetaTrader 5

Current (this) object as a parameter?

To add comments, please log in or register
Carry on your private correspondence in your profile. It is secure!
MrTwister
24
MrTwister 2015.08.08 21:48 

Hi

is there any way to use current object as a parameter? JavaScript have "this", what can i use to make same functionality in MQL4?

For example:

class Container {

public:
    
    Object *object;
    double m_Price;

    MyData(double price) { m_Price = price; }
    double getPrice(){ return m_Price; }

    void addObject(Container *container) {
        object = new Object(this);
    }

};

class Object {

public:
    Container *container;
    Object(Container *Container) {
        container = Container;
    }
    Container* whatIsYourContainer() {
        return container; 
    }
}
Amir Yacoby
1195
Amir Yacoby 2015.08.09 08:36  
MrTwister
24
MrTwister 2015.08.09 11:22  
amir_avatar:
Use GetPointer(this)
Great, thanks, forgot about it completely :)
whroeder1
14518
whroeder1 2015.08.09 12:12  
amir_avatar: Use GetPointer(this)
  1. Pass a pointer if you need to.
  2. Pass a reference would be preferred
    void fcn(Object& o){...}
    :
    fcn(this);

Amir Yacoby
1195
Amir Yacoby 2015.08.09 13:00  

not when 2 objects pointing at each other, then pointer is preffered

whroeder1
14518
whroeder1 2015.08.09 18:39  
amir_avatar: not when 2 objects pointing at each other, then pointer is preffered
You can't save a reference, there is no other option.
Amir Yacoby
1195
Amir Yacoby 2015.08.09 20:23  
WHRoeder:
You can't save a reference, there is no other option.

you can get its pointer and save it. but regardless, even if you have only 2 objects one has a reference to the other it wont compile. only pointers to each other.
MrTwister
24
MrTwister 2015.08.10 10:26  

I have problems with compiling. As i understand the compiler doesn't see one class before making another one. Container contains Object, but Object class doesn't exist when compiler needs it. What can i do with that situation?

//+------------------------------------------------------------------+
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict

class Container {

public:

    Object* object;
    double m_Price;

    Container(double price) { m_Price = price; }
    double getPrice(){ return m_Price; }

    void addObject() {
        object = new Object(GetPointer(this), m_Price);
        Object* obj = object.whatIsYourContainer();
        Print(obj.getPrice());
    }

};

class Object {

public:
    double objectPrice;
    Container* container;
    Object(Container* Container, double Price) {
        objectPrice = Price;
        container = Container;
    }
    Container* whatIsYourContainer() {
        return container;
    }
    double getPrice() {
      return objectPrice;
    }
}



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
Container* c = new Container(1.31200);
c.addObject();

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+

 

may be i need to declare the classes in some global space?

Error list: 

'Object' - declaration without type
'Container' - structure identifier cannot be used
'int' - semicolon expected
'int' - name expected
'c' - declaration without type
'return' - expressions are not allowed on a global scope
'}' - expressions are not allowed on a global scope
'object' - undeclared identifier
'=' - illegal operation use
'whatIsYourContainer' - object pointer expected
'Container' - structure identifier cannot be used
';' - unexpected token
'=' - operand expected
Amir Yacoby
1195
Amir Yacoby 2015.08.10 10:32  

you need forward definition of Object, before Container put:

class Object;

 

and then go on with

class Container {

public:
.....

 

I havn't tried it but it should work 

MrTwister
24
MrTwister 2015.08.10 10:53  
amir_avatar:

you need forward definition of Object, before Container put:

class Object;

 

and then go on with

class Container {

public:
.....

 

I havn't tried it but it should work 

Sadly no. If i just put Object class definition before Container it can't compile. If i add just one line class Object; it can't compile too.

class Container;

class Object {

public:
    double objectPrice;
    Container* container;
    Object(Container* Container, double Price) {
        objectPrice = Price;
        container = Container;
    }
    Container* whatIsYourContainer() {
        return container;
    }
    double getPrice() {
      return objectPrice;
    }
};


class Container {

public:

    Object* object;
    double m_Price;

    Container(double price) { m_Price = price; }
    double getPrice(){ return m_Price; }

    void addObject() {
        object = new Object(GetPointer(this), m_Price);
        Object* obj = object.whatIsYourContainer();
        Print(obj.getPrice());
    }

};

 errors:

'Container' - structure identifier cannot be used  
'Container' - structure identifier cannot be used  
';' - unexpected token 
'=' - operand expected 
'=' - type mismatch

 

 if i delete "class Container;" line there are more errors :-/

Amir Yacoby
1195
Amir Yacoby 2015.08.10 11:15  

What I meant is that you do like you did first time, but add forward class definition like this:

class Object;     //   <-- this line you need to add

class Container {

public:

    Object* object;
    double m_Price;

    Container(double price) { m_Price = price; }
    double getPrice(){ return m_Price; }

    void addObject() {
        object = new Object(GetPointer(this), m_Price);
        Object* obj = object.whatIsYourContainer();
        Print(obj.getPrice());
    }

};

class Object {

public:
    double objectPrice;
    Container* container;
    Object(Container* Container, double Price) {
        objectPrice = Price;
        container = Container;
    }
    Container* whatIsYourContainer() {
        return container;
    }
    double getPrice() {
      return objectPrice;
    }
}
 

12
To add comments, please log in or register