c++ - Dynamically allocating array of objects like int -
in order create array of int
s dynamically, need int *a = new int[20];
.
is there similar way create array of objects or structs?
for example, need dynamically allocated array of objects of class a
, how should declare it(syntax)?
what constructor called on each object(if @ constructor called)?
you can using line:
a* = new a[n];
how works?
the new
keyword allocate n
sequential block in heap. each block has size of sizeof(a)
total size in bytes n*sizeof(a)
. allocating objects in memory ensured done calling default constructor n
times.
alternative:
use std::vector
instead. work you.
Comments
Post a Comment