Download MetaTrader 5

Is self reference is possible with MQL OOP Programing?

To add comments, please log in or register
Pisit Wongmaungsan
125
Pisit Wongmaungsan  
class Child 
{
   Parent *parent;   
   public:
   void setParent(Parent *p) { parent = p; };      
};

class Parent
{
   Child *child;
   
   public:
      Parent()
      {
         child = new Child();
         child.setParent(this); // << Can we do something like this?
      }
      ~Parent();
};
 

I need to create two classes that has child & parent relationship
parent class can create their child within constructor and set child parent to itself.
This can be possible with MQL4?

ps. Sorry, my English is not good.



Geester
159
Geester  
Pisit Wongmaungsan:
 

I need to create two classes that has child & parent relationship
parent class can create their child within constructor and set child parent to itself.
This can be possible with MQL4?

ps. Sorry, my English is not good.




Hi @Pisit Wongmaungsan - I'm fairly new to MQL4 OOP programming so, please bear that in mind. Without commenting on the efficacy of your overall solution, from a higher level language, like C#, you can obviously pass "this" in the manner you have specified.

In your Child class, you are using a pointer of type Parent. I think you would need to do the following to get what you want:

child.setParent(GetPointer(this));

Take a look at https://docs.mql4.com/common/getpointer

Hope this helps.



GetPointer - Common Functions - MQL4 Reference
GetPointer - Common Functions - MQL4 Reference
  • docs.mql4.com
Only class objects have pointers. Instances of structures and simple-type variables can't have pointers. The class object not created using the new() operator, but, e.g., automatically created in the array of objects, still has a pointer. But this pointer will be of the automatic type POINTER_AUTOMATIC, therefore the delete() operator can't be...
whroeder1
17092
whroeder1  
Geester: I think you would need to do the following to get what you want:
child.setParent(GetPointer(this));
That works but I would "hide" the detail that the child remembers:
void setParent(Parent &p) { parent = GetPointer(p); };
:
child.setParent(this);
And it prevents passing a NULL pointer to the child.
Pisit Wongmaungsan
125
Pisit Wongmaungsan  
Geester:

GetPointer(this)



That what I'm looking for. it solves my problem. Thank you so much.
Geester
159
Geester  
Pisit Wongmaungsan:

That what I'm looking for. it solves my problem. Thank you so much.

Glad to help :)

nicholishen
1537
nicholishen  
Pisit Wongmaungsan:

That what I'm looking for. it solves my problem. Thank you so much.

You can also use

&this

I also use this pattern a lot, but pass the pointer through the constructor instead. 

class Parent;

class Child 
{
   Parent *m_parent;   
 public:
   Child(Parent *p) { m_parent = p; }
};

class Parent
{
   Child  m_child;  
 public:
   Parent():m_child(&this){}
  
};
Pisit Wongmaungsan
125
Pisit Wongmaungsan  
nicholishen:

You can also use

I also use this pattern a lot, but pass the pointer through the constructor instead. 


wow. I have never seen this technique before.
Thanks for more info & suggestion. 

To add comments, please log in or register