Current (this) object as a parameter?

 

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_avatar:
Use GetPointer(this)
Great, thanks, forgot about it completely :)
 
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);

 

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

 
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.
 
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.
 

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
 

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 

 
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 :-/

 

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;
    }
}