Download MetaTrader 5

Declare an array of class objects

To add comments, please log in or register
katexing
37
katexing  

Is it possible to declare an array of class objects without specifying how many there will be?

Like example i want to create an instance of a Class called CTickets, i do

CTickets e_ticket;

To create an array of 3 instances i do 

CTickets e_ticket[3] 

What if i don't know how many instances i will have? Can i create an array? What is the best way? I want to iterate the tickets thus i think an array of instances is the best. How do i do it? The purpose is that i would like to have exit strategies for each ticket.

 

Thanks. 

whroeder1
17116
whroeder1  
  1. CTickets e_ticket[];  ArrayResize(e_ticket, n);
    Remember that CTickets must have a default constructor.
  2. For style, the class should be singular, (it holds a single ticket,) the array should be plural as it holds many CTickets.
katexing
37
katexing  
whroeder1:
  1. CTickets e_ticket[];  ArrayResize(e_ticket, n);
    Remember that CTickets must have a default constructor.
  2. For style, the class should be singular, (it holds a single ticket,) the array should be plural as it holds many CTickets.
Thanks!
Stanislav Korotky
23391
Stanislav Korotky  
whroeder1:
  1. CTickets e_ticket[];  ArrayResize(e_ticket, n);
    Remember that CTickets must have a default constructor.
Why is that? The fact that you resize the array does not mean the elements will be automatically populated with fresh objects created with default constructor for you. You need create them explicitly, not necessarily with default constructor.
whroeder1
17116
whroeder1  
Stanislav Korotky: Why is that?
Perhaps you should read the manual. I gave you the link. What part of:
but a default constructor is not declared, you can not declare the arrays of objects of this class.
is unclear?
Stanislav Korotky
23391
Stanislav Korotky  
whroeder1:
Perhaps you should read the manual. I gave you the link. What part of:
is unclear?
The sentence you quoted from the documentation is not applied to the case of dynamic arrays. As far as you mentioned default constructor as required ("Remember that CTickets must have a default constructor") just one line below your code using ArrayResize, you're simply wrong and misguide other users.
Alain Verleyen
Moderator
29869
Alain Verleyen  
Stanislav Korotky:
The sentence you quoted from the documentation is not applied to the case of dynamic arrays. As far as you mentioned default constructor as required ("Remember that CTickets must have a default constructor") just one line below your code using ArrayResize, you're simply wrong and misguide other users.

Actually you are both wrong

whroeder1:
  1. CTickets e_ticket[];  ArrayResize(e_ticket, n);
    Remember that CTickets must have a default constructor.

A default constructor is not needed, as if there is not the compiler will create one. You quoted documentation incorrectly, it says

"This means that if a parametric constructor is declared in a class, but a default constructor is not declared, you can not declare the arrays of objects of this class."

So it's just if a parametric constructor is declared, that you need to also declare a default constructor.


Stanislav Korotky:
Why is that? The fact that you resize the array does not mean the elements will be automatically populated with fresh objects created with default constructor for you. You need create them explicitly, not necessarily with default constructor.

Of course, it's what that means. It's an array of objects, not an array of pointers.

Stanislav Korotky
23391
Stanislav Korotky  
Alain Verleyen:


Of course, it's what that means. It's an array of objects, not an array of pointers.

Yes, you're right. My fault.
To add comments, please log in or register