Download MetaTrader 5

MQL4 Object definition with initialization possible?

To add comments, please log in or register
bucket
38
bucket  
P { margin-bottom: 0.21cm; }

I am using MT 4 Version 4.00 Build 840. I programmed a class with 2 different constructors in its own file. The default constructor (without parameters) is private and the public constructor (with parameters) is public. So far everything works OK. It makes no sense to create an object of this class until some data is calculated to give proper values during initialisation. That is why the default constructor is private.

But in my EA I did not manage to define a global variable without that an object is instantiated. I my case At the moment I have to pass a dummy value to the public constructor.

Is it possible to only define a variable of a class without object instantiation?

Alain Verleyen
Moderator
31185
Alain Verleyen  
Do you mean a static variable ?
bucket
38
bucket  

No, not a static variable. Lets assumed I have a class MyClass1. If I declare a global variable of this type in an Expert Advisor the default constructor would be called because all global variables are initialized in MQL4.

// Global Variables

int x;       // x will be initialized with 0

// The next statement would be an error because I made the default constructor privat

MyClass1 y; // y would be initialized by calling the default constructor

// I have to use something like this:

MyClass1 y(0, 0, ""); // This constructor is public

But I solved my problem now. I declare a pointer to an object of this class. I can create an object of MyClass1 when I want. But I have to delete the object myself, bevore the Expert Advisor ends.

// Global Variables

int x;       // x will be initialized with 0

MyClass1 *y; // y will be initialized with NULL

...


int OnInit()
  {

     ... do some stuff

     y=new MyClass1(1, 400, "USD")    // calling the public constructor that needs 3 arguments

  }


void OnDeinit(const int reason)
  {
   delete(y);                        // It is mandatory to delete the dynamic object

  }

Alain Verleyen
Moderator
31185
Alain Verleyen  
bucket:

No, not a static variable. Lets assumed I have a class MyClass1. If I declare a global variable of this type in an Expert Advisor the default constructor would be called because all global variables are initialized in MQL4.

// Global Variables

int x;       // x will be initialized with 0

// The next statement would be an error because I made the default constructor privat

MyClass1 y; // y would be initialized by calling the default constructor

// I have to use something like this:

MyClass1 y(0, 0, ""); // This constructor is public

But I solved my problem now. I declare a pointer to an object of this class. I can create an object of MyClass1 when I want. But I have to delete the object myself, bevore the Expert Advisor ends.

// Global Variables

int x;       // x will be initialized with 0

MyClass1 *y; // y will be initialized with NULL

...


int OnInit()
  {

     ... do some stuff

     y=new MyClass1(1, 400, "USD")    // calling the public constructor that needs 3 arguments

  }


void OnDeinit(const int reason)
  {
   delete(y);                        // It is mandatory to delete the dynamic object

  }

Great. Sorry I have misunderstood your question.
To add comments, please log in or register