Declare an array of class objects

 

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. 

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

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